/**
 * Vitest global setup. The teardown phase runs after the test run completes
 * (whether passing or failing) and purges any `test-port-*` rows the
 * `makePort()` factory created during the run, plus the dependent rows
 * those ports own.
 *
 * Without this, integration tests leak hundreds of test ports per run; we
 * once accumulated >17k such rows in dev, slowing every page load that
 * fetches the port-switcher list.
 */

// `globalSetup` runs in vitest's parent process, so the test workers' env
// from `loadEnv` doesn't apply here - we have to load .env ourselves before
// importing the db module (which validates DATABASE_URL at import time).
import 'dotenv/config';
import { sql } from 'drizzle-orm';
import { db, closeDb } from '@/lib/db';

export async function setup() {
  // No-op: per-suite setup happens inside individual test files.
}

export async function teardown() {
  // Same DB as the dev/test environment - only delete obvious test rows
  // (slug prefix is a marker the factory always sets).
  await db.execute(sql`
    -- Stage the doomed port ids
    WITH doomed AS (
      SELECT id FROM ports WHERE slug LIKE 'test-port-%'
    )
    -- Cascade-delete dependent rows. Order respects FK chains.
    , del_audit AS (DELETE FROM audit_logs WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_ws AS (DELETE FROM website_submissions WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_bml AS (DELETE FROM berth_maintenance_log WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_resv AS (DELETE FROM berth_tenancies WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_caddr AS (DELETE FROM client_addresses WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_cmlog AS (DELETE FROM client_merge_log WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_crel AS (DELETE FROM client_relationships WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_compaddr AS (DELETE FROM company_addresses WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_comp AS (DELETE FROM companies WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_cfd AS (DELETE FROM custom_field_definitions WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_docs AS (DELETE FROM documents WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_dfold AS (DELETE FROM document_folders WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_dt AS (DELETE FROM document_templates WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_emt AS (DELETE FROM email_threads WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_ema AS (DELETE FROM email_accounts WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_exp AS (DELETE FROM expenses WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_files AS (DELETE FROM files WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_ft AS (DELETE FROM form_templates WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_gr AS (DELETE FROM generated_reports WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_icl AS (DELETE FROM interest_contact_log WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_int AS (DELETE FROM interests WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_ib AS (DELETE FROM interest_berths WHERE berth_id IN (SELECT id FROM berths WHERE port_id IN (SELECT id FROM doomed)) RETURNING 1)
    , del_inv AS (DELETE FROM invoices WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_notif AS (DELETE FROM notifications WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_pro AS (DELETE FROM port_role_overrides WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_pu AS (DELETE FROM portal_users WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_rem AS (DELETE FROM reminders WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_rc AS (DELETE FROM residential_clients WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_ri AS (DELETE FROM residential_interests WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_sv AS (DELETE FROM saved_views WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_sr AS (DELETE FROM scheduled_reports WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_ss AS (DELETE FROM system_settings WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_tags AS (DELETE FROM tags WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_unp AS (DELETE FROM user_notification_preferences WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_upr AS (DELETE FROM user_port_roles WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_wh AS (DELETE FROM webhooks WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_yachts AS (DELETE FROM yachts WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_berths AS (DELETE FROM berths WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_clients AS (DELETE FROM clients WHERE port_id IN (SELECT id FROM doomed) RETURNING 1)
    , del_ports AS (DELETE FROM ports WHERE id IN (SELECT id FROM doomed) RETURNING 1)
    SELECT 1
  `);
  // migration_source_links has no port_id FK; purge test-only ledger rows by
  // the marker applied_id our alert tests use.
  await db.execute(sql`DELETE FROM migration_source_links WHERE applied_id = 'test-apply'`);
  await closeDb();
}
