import { QueryInterface, QueryTypes } from 'sequelize';

/**
 * Migration to add AI model configuration entries
 * Adds CLAUDE_MODEL, OPENAI_MODEL, and GEMINI_MODEL to admin_configurations
 * 
 * This migration is idempotent - it will only insert if the configs don't exist.
 * For existing databases, this ensures the new model configuration fields are available.
 * For fresh databases, the seed scripts will handle the initial population.
 */
export async function up(queryInterface: QueryInterface): Promise<void> {
  // Insert AI model configurations if they don't exist
  await queryInterface.sequelize.query(`
    INSERT INTO admin_configurations (
      config_id, config_key, config_category, config_value, value_type,
      display_name, description, default_value, is_editable, is_sensitive,
      validation_rules, ui_component, options, sort_order, requires_restart,
      last_modified_by, last_modified_at, created_at, updated_at
    ) VALUES
    (
      gen_random_uuid(),
      'CLAUDE_MODEL',
      'AI_CONFIGURATION',
      'claude-sonnet-4-20250514',
      'STRING',
      'Claude Model',
      'Claude (Anthropic) model to use for AI generation',
      'claude-sonnet-4-20250514',
      true,
      false,
      '{}'::jsonb,
      'input',
      NULL,
      27,
      false,
      NULL,
      NULL,
      NOW(),
      NOW()
    ),
    (
      gen_random_uuid(),
      'OPENAI_MODEL',
      'AI_CONFIGURATION',
      'gpt-4o',
      'STRING',
      'OpenAI Model',
      'OpenAI model to use for AI generation',
      'gpt-4o',
      true,
      false,
      '{}'::jsonb,
      'input',
      NULL,
      28,
      false,
      NULL,
      NULL,
      NOW(),
      NOW()
    ),
    (
      gen_random_uuid(),
      'GEMINI_MODEL',
      'AI_CONFIGURATION',
      'gemini-2.0-flash-lite',
      'STRING',
      'Gemini Model',
      'Gemini (Google) model to use for AI generation',
      'gemini-2.0-flash-lite',
      true,
      false,
      '{}'::jsonb,
      'input',
      NULL,
      29,
      false,
      NULL,
      NULL,
      NOW(),
      NOW()
    )
    ON CONFLICT (config_key) DO NOTHING
  `, { type: QueryTypes.INSERT });
}

export async function down(queryInterface: QueryInterface): Promise<void> {
  // Remove the AI model configurations
  await queryInterface.sequelize.query(`
    DELETE FROM admin_configurations 
    WHERE config_key IN ('CLAUDE_MODEL', 'OPENAI_MODEL', 'GEMINI_MODEL')
  `, { type: QueryTypes.DELETE });
}

