/* eslint-disable no-console */
import { createIndexIfNotExists, type Migration } from '../migrate';

export const up: Migration = async ({ context }) => {
  console.log('🚀 Starting database performance optimization...');

  // meter_events
  await createIndexIfNotExists(context, 'meter_events', ['status', 'created_at'], 'idx_meter_events_status_created');
  await createIndexIfNotExists(
    context,
    'meter_events',
    ['event_name', 'livemode', 'created_at'],
    'idx_meter_events_event_livemode_created'
  );

  await context.sequelize.query(
    "CREATE INDEX IF NOT EXISTS idx_meter_events_customer_status ON meter_events(json_extract(payload, '$.customer_id'), status, livemode, created_at)"
  );
  await context.sequelize.query(
    "CREATE INDEX IF NOT EXISTS idx_meter_events_subscription_status ON meter_events(json_extract(payload, '$.subscription_id'), status, livemode, created_at)"
  );

  // credit_transactions
  await createIndexIfNotExists(context, 'credit_transactions', ['created_at'], 'idx_credit_transactions_created');
  await createIndexIfNotExists(
    context,
    'credit_transactions',
    ['customer_id', 'created_at'],
    'idx_credit_transactions_customer_created'
  );
  await createIndexIfNotExists(
    context,
    'credit_transactions',
    ['meter_id', 'created_at'],
    'idx_credit_transactions_meter_created'
  );
  await createIndexIfNotExists(
    context,
    'credit_transactions',
    ['meter_event_name', 'created_at'],
    'idx_credit_transactions_event_created'
  );
  await createIndexIfNotExists(
    context,
    'credit_transactions',
    ['subscription_id', 'created_at'],
    'idx_credit_transactions_subscription_created'
  );

  // credit_grants
  await createIndexIfNotExists(
    context,
    'credit_grants',
    ['customer_id', 'currency_id', 'status'],
    'idx_credit_grants_customer_currency_status'
  );
  await createIndexIfNotExists(context, 'credit_grants', ['customer_id'], 'idx_credit_grants_customer');
  await createIndexIfNotExists(context, 'credit_grants', ['currency_id'], 'idx_credit_grants_currency');
  await createIndexIfNotExists(context, 'credit_grants', ['status'], 'idx_credit_grants_status');

  // customers
  await createIndexIfNotExists(context, 'customers', ['did'], 'idx_customers_did');

  // subscriptions
  await createIndexIfNotExists(context, 'subscriptions', ['customer_id'], 'idx_subscriptions_customer_id');

  // meters
  await createIndexIfNotExists(context, 'meters', ['livemode', 'status'], 'idx_meters_livemode_status');

  // events
  await createIndexIfNotExists(context, 'events', ['type', 'created_at'], 'idx_events_type_created');
  await createIndexIfNotExists(
    context,
    'events',
    ['object_type', 'object_id', 'created_at'],
    'idx_events_object_type_id_created'
  );
  await createIndexIfNotExists(context, 'events', ['object_id', 'created_at'], 'idx_events_object_id_created');

  // jobs
  await createIndexIfNotExists(context, 'jobs', ['queue', 'will_run_at'], 'idx_jobs_queue_run_at');
  await createIndexIfNotExists(context, 'jobs', ['cancelled', 'will_run_at'], 'idx_jobs_cancelled_run_at');

  // invoices
  await createIndexIfNotExists(context, 'invoices', ['status'], 'idx_invoices_status');

  // checkout_sessions
  await createIndexIfNotExists(
    context,
    'checkout_sessions',
    ['payment_link_id', 'status', 'livemode'],
    'idx_checkout_sessions_payment_link_status_livemode'
  );

  console.log('✅ Performance optimization completed');
};

export const down: Migration = async ({ context }) => {
  console.log('🔄 Rolling back performance optimization...');

  await context.removeIndex('meter_events', 'idx_meter_events_status_created');
  await context.removeIndex('meter_events', 'idx_meter_events_event_livemode_created');
  await context.removeIndex('meter_events', 'idx_meter_events_customer_status');
  await context.removeIndex('meter_events', 'idx_meter_events_subscription_status');

  await context.removeIndex('credit_transactions', 'idx_credit_transactions_created');
  await context.removeIndex('credit_transactions', 'idx_credit_transactions_customer_created');
  await context.removeIndex('credit_transactions', 'idx_credit_transactions_meter_created');
  await context.removeIndex('credit_transactions', 'idx_credit_transactions_event_created');
  await context.removeIndex('credit_transactions', 'idx_credit_transactions_subscription_created');

  await context.removeIndex('credit_grants', 'idx_credit_grants_customer_currency_status');
  await context.removeIndex('credit_grants', 'idx_credit_grants_customer');
  await context.removeIndex('credit_grants', 'idx_credit_grants_currency');
  await context.removeIndex('credit_grants', 'idx_credit_grants_status');

  await context.removeIndex('customers', 'idx_customers_did');
  await context.removeIndex('subscriptions', 'idx_subscriptions_customer_id');
  await context.removeIndex('meters', 'idx_meters_livemode_status');
  await context.removeIndex('events', 'idx_events_type_created');
  await context.removeIndex('events', 'idx_events_object_type_id_created');
  await context.removeIndex('events', 'idx_events_object_id_created');
  await context.removeIndex('jobs', 'idx_jobs_queue_run_at');
  await context.removeIndex('jobs', 'idx_jobs_cancelled_run_at');
  await context.removeIndex('invoices', 'idx_invoices_status');
  await context.removeIndex('checkout_sessions', 'idx_checkout_sessions_payment_link_status_livemode');

  console.log('✅ Rollback completed');
};
