import { db } from "@/db";
import {
  offer_phases,
  offer_phase_services,
  quotes,
  quote_items,
  projects,
  phases,
  leads,
} from "@/db/schema";
import { sql } from "drizzle-orm";

/**
 * Validation script for Phase 8 migration
 *
 * Checks that all new tables/columns exist and FKs are intact
 * Exit code 0 if all checks pass, 1 if any fail
 */

type ValidationResult = {
  name: string;
  passed: boolean;
  error?: string;
};

const results: ValidationResult[] = [];

async function runChecks() {
  console.log("=== Phase 8 Migration Validation ===\n");

  // Check 1: offer_phases table exists
  try {
    const phaseCount = await db
      .select({ count: sql<number>`COUNT(*)` })
      .from(offer_phases);
    results.push({
      name: "offer_phases table exists",
      passed: true,
      error: `(${phaseCount[0].count} rows)`,
    });
  } catch (e) {
    results.push({
      name: "offer_phases table exists",
      passed: false,
      error: (e as Error).message,
    });
  }

  // Check 2: offer_phase_services table exists
  try {
    const serviceCount = await db
      .select({ count: sql<number>`COUNT(*)` })
      .from(offer_phase_services);
    results.push({
      name: "offer_phase_services table exists",
      passed: true,
      error: `(${serviceCount[0].count} rows)`,
    });
  } catch (e) {
    results.push({
      name: "offer_phase_services table exists",
      passed: false,
      error: (e as Error).message,
    });
  }

  // Check 3: quotes table exists
  try {
    const quoteCount = await db
      .select({ count: sql<number>`COUNT(*)` })
      .from(quotes);
    results.push({
      name: "quotes table exists",
      passed: true,
      error: `(${quoteCount[0].count} rows)`,
    });
  } catch (e) {
    results.push({
      name: "quotes table exists",
      passed: false,
      error: (e as Error).message,
    });
  }

  // Check 4: leads table exists
  try {
    const leadCount = await db
      .select({ count: sql<number>`COUNT(*)` })
      .from(leads);
    results.push({
      name: "leads table exists",
      passed: true,
      error: `(${leadCount[0].count} rows)`,
    });
  } catch (e) {
    results.push({
      name: "leads table exists",
      passed: false,
      error: (e as Error).message,
    });
  }

  // Check 5: quote_items.quote_id column exists
  try {
    const col = await db
      .select({ column_name: sql<string>`column_name` })
      .from(
        sql`information_schema.columns`
      )
      .where(
        sql`table_name = 'quote_items' AND column_name = 'quote_id'`
      );
    results.push({
      name: "quote_items.quote_id column exists",
      passed: col.length > 0,
      error: col.length > 0 ? undefined : "Column not found",
    });
  } catch (e) {
    results.push({
      name: "quote_items.quote_id column exists",
      passed: false,
      error: (e as Error).message,
    });
  }

  // Check 6: quote_items.offer_micro_id column exists
  try {
    const col = await db
      .select({ column_name: sql<string>`column_name` })
      .from(
        sql`information_schema.columns`
      )
      .where(
        sql`table_name = 'quote_items' AND column_name = 'offer_micro_id'`
      );
    results.push({
      name: "quote_items.offer_micro_id column exists",
      passed: col.length > 0,
      error: col.length > 0 ? undefined : "Column not found",
    });
  } catch (e) {
    results.push({
      name: "quote_items.offer_micro_id column exists",
      passed: false,
      error: (e as Error).message,
    });
  }

  // Check 7: quote_items.offer_phase_id column exists
  try {
    const col = await db
      .select({ column_name: sql<string>`column_name` })
      .from(
        sql`information_schema.columns`
      )
      .where(
        sql`table_name = 'quote_items' AND column_name = 'offer_phase_id'`
      );
    results.push({
      name: "quote_items.offer_phase_id column exists",
      passed: col.length > 0,
      error: col.length > 0 ? undefined : "Column not found",
    });
  } catch (e) {
    results.push({
      name: "quote_items.offer_phase_id column exists",
      passed: false,
      error: (e as Error).message,
    });
  }

  // Check 8: projects.offer_id column exists
  try {
    const col = await db
      .select({ column_name: sql<string>`column_name` })
      .from(
        sql`information_schema.columns`
      )
      .where(
        sql`table_name = 'projects' AND column_name = 'offer_id'`
      );
    results.push({
      name: "projects.offer_id column exists",
      passed: col.length > 0,
      error: col.length > 0 ? undefined : "Column not found",
    });
  } catch (e) {
    results.push({
      name: "projects.offer_id column exists",
      passed: false,
      error: (e as Error).message,
    });
  }

  // Check 9: projects.created_from_lead_id column exists
  try {
    const col = await db
      .select({ column_name: sql<string>`column_name` })
      .from(
        sql`information_schema.columns`
      )
      .where(
        sql`table_name = 'projects' AND column_name = 'created_from_lead_id'`
      );
    results.push({
      name: "projects.created_from_lead_id column exists",
      passed: col.length > 0,
      error: col.length > 0 ? undefined : "Column not found",
    });
  } catch (e) {
    results.push({
      name: "projects.created_from_lead_id column exists",
      passed: false,
      error: (e as Error).message,
    });
  }

  // Check 10: phases.offer_phase_id column exists
  try {
    const col = await db
      .select({ column_name: sql<string>`column_name` })
      .from(
        sql`information_schema.columns`
      )
      .where(
        sql`table_name = 'phases' AND column_name = 'offer_phase_id'`
      );
    results.push({
      name: "phases.offer_phase_id column exists",
      passed: col.length > 0,
      error: col.length > 0 ? undefined : "Column not found",
    });
  } catch (e) {
    results.push({
      name: "phases.offer_phase_id column exists",
      passed: false,
      error: (e as Error).message,
    });
  }

  // Print results
  let allPassed = true;
  console.log("Checks:\n");
  for (const result of results) {
    const status = result.passed ? "✓" : "✗";
    const msg = result.error
      ? ` ${result.error}`
      : " passed";
    console.log(`${status} ${result.name}${msg}`);
    if (!result.passed) allPassed = false;
  }

  console.log(
    `\n=== Result: ${allPassed ? "ALL CHECKS PASSED" : "SOME CHECKS FAILED"} ===`
  );
  process.exit(allPassed ? 0 : 1);
}

runChecks().catch((e) => {
  console.error("Validation script error:", e);
  process.exit(1);
});
