-- Migration: Add additional_recipients field to notifications table
-- Description: Menambahkan field untuk menyimpan email recipients tambahan selain email employee
-- Date: 2026-02-06
-- Safe: Yes (menambahkan field nullable, tidak merusak data existing)

-- Check if column exists before adding
SET @dbname = DATABASE();
SET @tablename = 'notifications';
SET @columnname = 'additional_recipients';
SET @preparedStatement = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (TABLE_SCHEMA = @dbname)
      AND (TABLE_NAME = @tablename)
      AND (COLUMN_NAME = @columnname)
  ) > 0,
  'SELECT 1', -- Column exists, do nothing
  CONCAT('ALTER TABLE ', @tablename, ' ADD COLUMN ', @columnname, ' JSON NULL COMMENT "Additional email recipients (array of email addresses) for notifications" AFTER recipient_emails')
));
PREPARE alterIfNotExists FROM @preparedStatement;
EXECUTE alterIfNotExists;
DEALLOCATE PREPARE alterIfNotExists;

-- Add index for better query performance (optional, but recommended)
-- Note: JSON columns can't be indexed directly in MySQL, but we can add a generated column if needed
-- For now, we'll skip indexing as JSON queries are typically done on small datasets

-- Verify the column was added
SELECT 
    COLUMN_NAME,
    DATA_TYPE,
    IS_NULLABLE,
    COLUMN_DEFAULT,
    COLUMN_COMMENT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE 
    TABLE_SCHEMA = @dbname
    AND TABLE_NAME = @tablename
    AND COLUMN_NAME = @columnname;
