-- Migration: Support Multiple Email Providers (SAFE VERSION)
-- This migration allows storing multiple provider configurations per channel type

-- STEP 0: First, check what indexes exist on the table
-- Run this query to see existing indexes:
SHOW INDEX FROM notification_delivery_config;

-- You should see output showing index names. Look for a unique index on 'channel_type'
-- Common names: 'channel_type', 'notification_delivery_config_channel_type_key', or similar

-- STEP 1: Drop the unique constraint on channel_type
-- Option A: If the index is named 'channel_type'
-- ALTER TABLE notification_delivery_config DROP INDEX channel_type;

-- Option B: If you don't have a unique constraint yet, skip step 1

-- Check if constraint exists before dropping (MySQL 5.7+)
SET @exist := (SELECT COUNT(*) FROM information_schema.statistics
               WHERE table_schema = DATABASE()
               AND table_name = 'notification_delivery_config'
               AND index_name = 'channel_type');
SET @sqlstmt := IF(@exist > 0,
                   'ALTER TABLE notification_delivery_config DROP INDEX channel_type',
                   'SELECT "Index does not exist, skipping..."');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- STEP 2: Add unique constraint on (channel_type, provider) combination
-- First check if it already exists
SET @exist := (SELECT COUNT(*) FROM information_schema.statistics
               WHERE table_schema = DATABASE()
               AND table_name = 'notification_delivery_config'
               AND index_name = 'unique_channel_provider');
SET @sqlstmt := IF(@exist = 0,
                   'ALTER TABLE notification_delivery_config ADD UNIQUE KEY unique_channel_provider (channel_type, provider)',
                   'SELECT "Unique index already exists, skipping..."');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- STEP 3: Add is_active column if it doesn't exist
SET @exist := (SELECT COUNT(*) FROM information_schema.columns
               WHERE table_schema = DATABASE()
               AND table_name = 'notification_delivery_config'
               AND column_name = 'is_active');
SET @sqlstmt := IF(@exist = 0,
                   'ALTER TABLE notification_delivery_config ADD COLUMN is_active BOOLEAN DEFAULT false AFTER is_enabled',
                   'SELECT "Column is_active already exists, skipping..."');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- STEP 4: Set current email config as active (if exists)
UPDATE notification_delivery_config
SET is_active = true
WHERE channel_type = 'email'
  AND is_enabled = true
LIMIT 1;

-- STEP 5: Insert Mailtrap configuration if not exists
INSERT IGNORE INTO notification_delivery_config
(channel_type, is_enabled, is_active, provider, provider_config, status, success_rate, created_by, updated_by, created_at, updated_at)
VALUES (
  'email',
  false,
  false,
  'Mailtrap',
  JSON_OBJECT(
    'host', 'live.smtp.mailtrap.io',
    'port', 587,
    'secure', false,
    'user', 'apismtp@mailtrap.io',
    'pass', '',
    'senderEmail', '',
    'senderName', ''
  ),
  'Not Configured',
  0.0,
  1,
  1,
  NOW(),
  NOW()
);

-- STEP 6: Insert AWS SES configuration if not exists
INSERT IGNORE INTO notification_delivery_config
(channel_type, is_enabled, is_active, provider, provider_config, status, success_rate, created_by, updated_by, created_at, updated_at)
VALUES (
  'email',
  false,
  false,
  'AWS SES',
  JSON_OBJECT(
    'host', '',
    'port', 587,
    'secure', false,
    'user', '',
    'pass', '',
    'senderEmail', '',
    'senderName', '',
    'region', 'us-east-1',
    'configurationSet', ''
  ),
  'Not Configured',
  0.0,
  1,
  1,
  NOW(),
  NOW()
);

-- STEP 7: Verify the migration
SELECT
  id,
  channel_type,
  provider,
  is_enabled,
  is_active,
  status,
  JSON_EXTRACT(provider_config, '$.host') as smtp_host,
  created_at,
  updated_at
FROM notification_delivery_config
WHERE channel_type = 'email'
ORDER BY provider;
