// FU-002 — Wave-13 demo seeder
//
// Seeds the minimum demo data needed for a brand-new tenant to demonstrate a
// PASS path through the wave13-smoke `vendor-register` module. Without this
// seeder, an empty `vendor_register` table will (correctly) fail the control,
// because the control cannot evidence vendor due-diligence on a vendor list
// that is empty.
//
// Demo records are tagged with the `DEMO-` vendor_ref prefix so they are
// trivially filterable for a regulator distinguishing demo evidence from
// real-world vendor onboarding.
//
// Idempotent: re-running the seeder is a no-op once the records exist.

import { db } from "../server/db";
import { vendorRegister } from "@shared/schema-wave13";
import { eq } from "drizzle-orm";

const DEMO_VENDOR_REF_PREFIX = "DEMO-VENDOR-";

interface DemoVendor {
  vendorRef: string;
  vendorName: string;
  serviceCategory: string;
  criticality: string;
  country: string;
  contractStartIsoOffsetMonths: number; // negative = past
  contractEndIsoOffsetMonths: number;   // positive = future
}

const DEMO_VENDORS: DemoVendor[] = [
  {
    vendorRef: `${DEMO_VENDOR_REF_PREFIX}001`,
    vendorName: "AcmeCloud DR (demo)",
    serviceCategory: "cloud-infrastructure",
    criticality: "critical",
    country: "DE",
    contractStartIsoOffsetMonths: -8,
    contractEndIsoOffsetMonths: 16,
  },
  {
    vendorRef: `${DEMO_VENDOR_REF_PREFIX}002`,
    vendorName: "SafeChain Sanctions API (demo)",
    serviceCategory: "sanctions-screening",
    criticality: "high",
    country: "GB",
    contractStartIsoOffsetMonths: -4,
    contractEndIsoOffsetMonths: 20,
  },
  {
    vendorRef: `${DEMO_VENDOR_REF_PREFIX}003`,
    vendorName: "BoU SupTech Gateway (demo)",
    serviceCategory: "regulatory-reporting",
    criticality: "medium",
    country: "UG",
    contractStartIsoOffsetMonths: -12,
    contractEndIsoOffsetMonths: 12,
  },
];

function offsetMonthsFromNow(months: number): Date {
  const d = new Date();
  d.setMonth(d.getMonth() + months);
  return d;
}

export interface SeedResult {
  inserted: number;
  skipped: number;
  vendorRefs: string[];
}

export async function seedWave13Demo(triggeredBy: string): Promise<SeedResult> {
  let inserted = 0;
  let skipped = 0;
  const vendorRefs: string[] = [];

  for (const v of DEMO_VENDORS) {
    const existing = await db
      .select({ id: vendorRegister.id })
      .from(vendorRegister)
      .where(eq(vendorRegister.vendorRef, v.vendorRef))
      .limit(1);

    if (existing.length > 0) {
      skipped += 1;
      vendorRefs.push(v.vendorRef);
      continue;
    }

    await db.insert(vendorRegister).values({
      vendorRef: v.vendorRef,
      vendorName: v.vendorName,
      serviceCategory: v.serviceCategory,
      criticality: v.criticality,
      country: v.country,
      contractStart: offsetMonthsFromNow(v.contractStartIsoOffsetMonths),
      contractEnd: offsetMonthsFromNow(v.contractEndIsoOffsetMonths),
      exitClauseDocumented: 1,
      active: 1,
      registeredBy: triggeredBy,
    });
    inserted += 1;
    vendorRefs.push(v.vendorRef);
  }

  return { inserted, skipped, vendorRefs };
}
