/**
 * Acacia Communications OEM Transceiver Catalog Seed
 *
 * Seeds Acacia Communications (acquired by Cisco in 2021) branded coherent
 * optical module PIDs. Acacia pioneered silicon photonics-based coherent DSP
 * modules including the AC400, AC1200, and Pico platforms used in DCI,
 * metro, and long-haul optical transport networks.
 *
 * Sources:
 *  - Acacia AC400 CFP2-DCO 400G coherent module datasheet
 *  - Acacia AC1200 1.2T coherent module product brief
 *  - Acacia Pico 100G coherent CFP datasheet
 *
 * Run:  tsx packages/scraper/src/scrapers/acacia-oem.ts
 * Cron: daily at 03:00
 */

import { pool, ensureVendor } from "../utils/db";

interface AcaciaPID {
  pid: string;
  formFactor: string;
  speedGbps: number;
  speed: string;
  reachMeters: number;
  reachLabel: string;
  fiberType: string;
  connector: string;
  wavelengths?: string;
  standard?: string;
  notes?: string;
}

const ACACIA_PIDS: AcaciaPID[] = [
  // ── 100G Coherent (Pico / CFP) ───────────────────────────────────────
  { pid: "AC-PICO-100G-CFP",    formFactor: "CFP",    speedGbps: 100, speed: "100G", reachMeters: 1000000, reachLabel: "LH",    fiberType: "SMF", connector: "LC", wavelengths: "C-band",   notes: "Acacia Pico 100G coherent CFP, long-haul" },
  { pid: "AC-PICO-100G-CFP2",   formFactor: "CFP2",   speedGbps: 100, speed: "100G", reachMeters: 1000000, reachLabel: "LH",    fiberType: "SMF", connector: "LC", wavelengths: "C-band",   notes: "Acacia Pico 100G coherent CFP2" },
  { pid: "AC-PICO-100G-QSFP28", formFactor: "QSFP28", speedGbps: 100, speed: "100G", reachMeters: 80000,   reachLabel: "ZR",    fiberType: "SMF", connector: "LC", wavelengths: "C-band",   standard: "100G-ZR", notes: "Acacia Pico 100G ZR QSFP28" },

  // ── 200G Coherent ────────────────────────────────────────────────────
  { pid: "AC-CFP2-DCO-200G",    formFactor: "CFP2-DCO",speedGbps: 200,speed: "200G", reachMeters: 1000000, reachLabel: "LH",    fiberType: "SMF", connector: "LC", wavelengths: "C-band",   notes: "Acacia 200G CFP2-DCO coherent" },

  // ── 400G Coherent (AC400) ─────────────────────────────────────────────
  { pid: "AC400-CFP2-DCO",      formFactor: "CFP2-DCO",speedGbps: 400,speed: "400G", reachMeters: 1000000, reachLabel: "LH",    fiberType: "SMF", connector: "LC", wavelengths: "C-band",   notes: "Acacia AC400 400G CFP2-DCO — industry milestone" },
  { pid: "AC400-QSFPDD-ZR",     formFactor: "QSFP-DD", speedGbps: 400,speed: "400G", reachMeters: 120000,  reachLabel: "ZR",    fiberType: "SMF", connector: "LC", wavelengths: "C-band",   standard: "400ZR", notes: "Acacia 400G ZR QSFP-DD" },
  { pid: "AC400-QSFPDD-ZRP",    formFactor: "QSFP-DD", speedGbps: 400,speed: "400G", reachMeters: 1000000, reachLabel: "ZR+",   fiberType: "SMF", connector: "LC", wavelengths: "C-band",   notes: "Acacia 400G ZR+ QSFP-DD extended reach" },

  // ── 400G DataCenter (non-coherent) ───────────────────────────────────
  { pid: "AC-QSFPDD-400G-DR4",  formFactor: "QSFP-DD", speedGbps: 400,speed: "400G", reachMeters: 500,     reachLabel: "DR4",   fiberType: "SMF", connector: "MPO", wavelengths: "1310nm", standard: "400GBASE-DR4" },
  { pid: "AC-QSFPDD-400G-FR4",  formFactor: "QSFP-DD", speedGbps: 400,speed: "400G", reachMeters: 2000,    reachLabel: "FR4",   fiberType: "SMF", connector: "LC",  wavelengths: "1271-1331nm", standard: "400GBASE-FR4" },
  { pid: "AC-QSFPDD-400G-LR4",  formFactor: "QSFP-DD", speedGbps: 400,speed: "400G", reachMeters: 10000,   reachLabel: "LR4",   fiberType: "SMF", connector: "LC",  wavelengths: "1295-1310nm", standard: "400GBASE-LR4" },

  // ── 800G Coherent (AC1200 platform) ──────────────────────────────────
  { pid: "AC1200-800G-OSFP",    formFactor: "OSFP",   speedGbps: 800, speed: "800G", reachMeters: 1000000, reachLabel: "LH",    fiberType: "SMF", connector: "LC", wavelengths: "C-band",   notes: "Acacia AC1200 800G coherent OSFP" },
  { pid: "AC1200-1200G-OSFP",   formFactor: "OSFP",   speedGbps: 1200,speed: "1.2T", reachMeters: 1000000, reachLabel: "LH",    fiberType: "SMF", connector: "LC", wavelengths: "C-band",   notes: "Acacia AC1200 1.2T coherent OSFP — highest capacity" },

  // ── 100G standard (non-coherent, Acacia silicon photonics) ───────────
  { pid: "AC-QSFP28-100G-SR4",  formFactor: "QSFP28", speedGbps: 100, speed: "100G", reachMeters: 100,     reachLabel: "SR4",   fiberType: "MMF", connector: "MPO", wavelengths: "850nm", standard: "100GBASE-SR4" },
  { pid: "AC-QSFP28-100G-LR4",  formFactor: "QSFP28", speedGbps: 100, speed: "100G", reachMeters: 10000,   reachLabel: "LR4",   fiberType: "SMF", connector: "LC",  wavelengths: "1295-1310nm", standard: "100GBASE-LR4" },
];

export async function scrapeAcaciaOem(): Promise<void> {
  console.log("=== Acacia Communications OEM Transceiver Seed ===\n");

  const vendorId = await ensureVendor(
    "Acacia Communications (Cisco)",
    "oem",
    "https://acacia-inc.com",
    undefined
  );

  let inserted = 0, updated = 0, errors = 0;

  for (const p of ACACIA_PIDS) {
    const slug = `acacia-${p.pid.toLowerCase().replace(/[^a-z0-9]+/g, "-")}`;
    const category = p.speedGbps >= 200 && p.reachMeters > 10000 ? "Telecom" : "DataCenter";
    try {
      const res = await pool.query(
        `INSERT INTO transceivers
           (slug, part_number, vendor_id, form_factor, speed, speed_gbps,
            reach_meters, reach_label, fiber_type, connector, wavelengths,
            dom_support, ieee_reference, market_status, category, notes)
         VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,true,$12,'Mainstream',$13,$14)
         ON CONFLICT (slug) DO UPDATE SET
           speed_gbps   = EXCLUDED.speed_gbps,
           reach_meters = CASE WHEN EXCLUDED.reach_meters > 0 THEN EXCLUDED.reach_meters ELSE transceivers.reach_meters END,
           fiber_type   = CASE WHEN EXCLUDED.fiber_type <> '' THEN EXCLUDED.fiber_type ELSE transceivers.fiber_type END,
           wavelengths  = COALESCE(EXCLUDED.wavelengths, transceivers.wavelengths),
           updated_at   = NOW()
         RETURNING (xmax = 0) as was_inserted`,
        [slug, p.pid, vendorId, p.formFactor, p.speed, p.speedGbps,
         p.reachMeters, p.reachLabel, p.fiberType, p.connector,
         p.wavelengths ?? null, p.standard ?? null, category, p.notes ?? null]
      );
      if (res.rows[0]?.was_inserted) inserted++; else updated++;
    } catch (err) {
      console.warn(`  Skip ${p.pid}: ${(err as Error).message.slice(0, 80)}`);
      errors++;
    }
  }

  console.log(`\n=== Acacia OEM Seed Complete ===`);
  console.log(`  Inserted: ${inserted}, Updated: ${updated}, Errors: ${errors}`);
  console.log(`  Total PIDs: ${ACACIA_PIDS.length}\n`);
}

if (require.main === module) {
  scrapeAcaciaOem()
    .then(() => pool.end())
    .catch((err) => { console.error("Fatal:", err); pool.end(); process.exit(1); });
}
