import { Migration } from '../migrate.js'

export const up: Migration['up'] = async (db) => {
  // Add new product categories for Sovereign Stack services
  // We need to drop and recreate the constraint to add new categories
  await db.query(`
    ALTER TABLE products
    DROP CONSTRAINT IF EXISTS products_category_check
  `)

  await db.query(`
    ALTER TABLE products
    ADD CONSTRAINT products_category_check
    CHECK (category IN (
      'COMPUTE',
      'NETWORK_INFRA',
      'BLOCKCHAIN_STACK',
      'BLOCKCHAIN_TOOLS',
      'FINANCIAL_MESSAGING',
      'INTERNET_REGISTRY',
      'AI_LLM_AGENT',
      'LEDGER_SERVICES',
      'IDENTITY_SERVICES',
      'WALLET_SERVICES',
      'ORCHESTRATION_SERVICES',
      'PLATFORM_SERVICES'
    ))
  `)

  // Create Phoenix Cloud Services publisher if it doesn't exist
  await db.query(`
    INSERT INTO publishers (
      name,
      display_name,
      description,
      website_url,
      logo_url,
      verified,
      metadata,
      created_at,
      updated_at
    )
    VALUES (
      'phoenix-cloud-services',
      'Phoenix Cloud Services',
      'Sovereign cloud infrastructure provider powering the Sankofa ecosystem. Phoenix delivers world-class cloud services with multi-tenancy, sovereign identity, and advanced billing capabilities.',
      'https://phoenix.sankofa.nexus',
      'https://cdn.sankofa.nexus/phoenix-logo.svg',
      true,
      '{"provider": "phoenix", "tier": "sovereign", "regions": 325, "sovereign_identity": true}'::jsonb,
      NOW(),
      NOW()
    )
    ON CONFLICT (name) DO UPDATE SET
      display_name = EXCLUDED.display_name,
      description = EXCLUDED.description,
      website_url = EXCLUDED.website_url,
      logo_url = EXCLUDED.logo_url,
      verified = true,
      metadata = EXCLUDED.metadata,
      updated_at = NOW()
  `)
}

export const down: Migration['down'] = async (db) => {
  // Remove new categories, reverting to original set
  await db.query(`
    ALTER TABLE products
    DROP CONSTRAINT IF EXISTS products_category_check
  `)

  await db.query(`
    ALTER TABLE products
    ADD CONSTRAINT products_category_check
    CHECK (category IN (
      'COMPUTE',
      'NETWORK_INFRA',
      'BLOCKCHAIN_STACK',
      'BLOCKCHAIN_TOOLS',
      'FINANCIAL_MESSAGING',
      'INTERNET_REGISTRY',
      'AI_LLM_AGENT'
    ))
  `)

  // Note: We don't delete the Phoenix publisher in down migration
  // as it may have been created manually or have dependencies
}
