/**
 * Infinera Groove/ICE OEM Transceiver Catalog Seed
 *
 * Seeds Infinera INF-prefixed transceiver PIDs for the Groove G30/G42
 * disaggregated open line system and ICE4/ICE6 coherent engine platforms.
 * Covers standard grey optics, OpenZR+, ZR, OTN/OTU, muxponder modules,
 * and CFP2-DCO coherent transceivers used across the Infinera portfolio.
 *
 * Sources:
 *  - Infinera Groove G30/G42 Hardware Compatibility Guide (infinera.com)
 *  - Infinera ICE4/ICE6 Coherent Engine Module Reference
 *  - OIF 400ZR Implementation Agreement
 *  - ITU-T G.709 OTN specification
 *
 * Run:  tsx packages/scraper/src/scrapers/infinera-groove-oem.ts
 * Cron: daily at 23:00
 */

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

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

const INFINERA_GROOVE_PIDS: InfineraGroovePID[] = [
  // ── 1G SFP (management / client) ─────────────────────────────────────────
  { pid: "INF-SFP-GE-SX",        formFactor: "SFP",     speedGbps: 1,   speed: "1G",    reachMeters: 550,     reachLabel: "SX",        fiberType: "MMF", connector: "LC",  wavelengths: "850nm",       standard: "1000BASE-SX" },

  // ── 10G SFP+ client optics ────────────────────────────────────────────────
  { pid: "INF-SFP-10G-SR",        formFactor: "SFP+",    speedGbps: 10,  speed: "10G",   reachMeters: 300,     reachLabel: "SR",        fiberType: "MMF", connector: "LC",  wavelengths: "850nm",       standard: "10GBASE-SR" },
  { pid: "INF-SFP-10G-LR",        formFactor: "SFP+",    speedGbps: 10,  speed: "10G",   reachMeters: 10000,   reachLabel: "LR",        fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",      standard: "10GBASE-LR" },
  { pid: "INF-SFP-10G-ER",        formFactor: "SFP+",    speedGbps: 10,  speed: "10G",   reachMeters: 40000,   reachLabel: "ER",        fiberType: "SMF", connector: "LC",  wavelengths: "1550nm",      standard: "10GBASE-ER" },
  { pid: "INF-SFP-10G-ZR",        formFactor: "SFP+",    speedGbps: 10,  speed: "10G",   reachMeters: 80000,   reachLabel: "ZR",        fiberType: "SMF", connector: "LC",  wavelengths: "1550nm",      standard: "10GBASE-ZR",  notes: "10G ZR 80km grey optic for Groove G30 client side" },

  // ── OTN / OTU client optics (Telecom) ────────────────────────────────────
  { pid: "INF-OTU2-SFP-10G",      formFactor: "SFP+",    speedGbps: 10,  speed: "10G",   reachMeters: 10000,   reachLabel: "OTU2",      fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",      standard: "ITU-T G.709", notes: "OTU2 10G client SFP+ for Groove G30 OTN muxponder", isTelecom: true },
  { pid: "INF-OTU2E-SFP-10G",     formFactor: "SFP+",    speedGbps: 11,  speed: "11G",   reachMeters: 10000,   reachLabel: "OTU2e",     fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",      standard: "ITU-T G.709", notes: "OTU2e 11.09Gbps client SFP+ for 10GbE over OTN mapping", isTelecom: true },

  // ── 40G QSFP+ ────────────────────────────────────────────────────────────
  { pid: "INF-QSFP-40G-LR4",      formFactor: "QSFP+",   speedGbps: 40,  speed: "40G",   reachMeters: 10000,   reachLabel: "LR4",       fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",      standard: "40GBASE-LR4" },

  // ── 100G QSFP28 client optics ─────────────────────────────────────────────
  { pid: "INF-QSFP28-100G-SR4",    formFactor: "QSFP28",  speedGbps: 100, speed: "100G",  reachMeters: 100,     reachLabel: "SR4",       fiberType: "MMF", connector: "MPO", wavelengths: "850nm",       standard: "100GBASE-SR4" },
  { pid: "INF-QSFP28-100G-LR4",    formFactor: "QSFP28",  speedGbps: 100, speed: "100G",  reachMeters: 10000,   reachLabel: "LR4",       fiberType: "SMF", connector: "LC",  wavelengths: "1295-1310nm", standard: "100GBASE-LR4" },
  { pid: "INF-QSFP28-100G-ER4",    formFactor: "QSFP28",  speedGbps: 100, speed: "100G",  reachMeters: 40000,   reachLabel: "ER4",       fiberType: "SMF", connector: "LC",  wavelengths: "1295-1310nm", notes: "100G ER4 40km QSFP28 for Groove G42 inter-site client" },

  // ── GRV2 QSFP28 (Groove G30 integrated coherent) ─────────────────────────
  { pid: "INF-GRV2-QSFP28",        formFactor: "QSFP28",  speedGbps: 100, speed: "100G",  reachMeters: 2000000, reachLabel: "GRV2-Coh",  fiberType: "SMF", connector: "LC",  wavelengths: "C-band",      notes: "Groove G30 Gen2 integrated coherent QSFP28 DSP module, up to 2000km", isTelecom: true },

  // ── CFP2-DCO coherent (Groove G42 line side) ─────────────────────────────
  { pid: "INF-CFP2-DCO-100G",      formFactor: "CFP2",    speedGbps: 100, speed: "100G",  reachMeters: 2000000, reachLabel: "DCO-100G",  fiberType: "SMF", connector: "LC",  wavelengths: "C-band",      notes: "CFP2-DCO 100G coherent for Groove G42 open line system, up to 2000km", isTelecom: true },
  { pid: "INF-CFP2-DCO-200G",      formFactor: "CFP2",    speedGbps: 200, speed: "200G",  reachMeters: 1500000, reachLabel: "DCO-200G",  fiberType: "SMF", connector: "LC",  wavelengths: "C-band",      notes: "CFP2-DCO 200G coherent for Groove G42, up to 1500km dual-polarization QPSK", isTelecom: true },

  // ── OTU4 line optics (Telecom) ────────────────────────────────────────────
  { pid: "INF-CFP-OTU4-100G",      formFactor: "CFP",     speedGbps: 100, speed: "100G",  reachMeters: 2000000, reachLabel: "OTU4",      fiberType: "SMF", connector: "LC",  wavelengths: "C-band",      standard: "ITU-T G.709", notes: "CFP OTU4 100G coherent line module for legacy Infinera DTN-X platform", isTelecom: true },

  // ── 400G QSFP-DD (ICE4 / Groove G42) ─────────────────────────────────────
  { pid: "INF-QSFP-DD-400G-ZR",    formFactor: "QSFP-DD", speedGbps: 400, speed: "400G",  reachMeters: 120000,  reachLabel: "ZR",        fiberType: "SMF", connector: "LC",  wavelengths: "C-band",      standard: "400ZR",       notes: "400G OpenZR coherent QSFP-DD for ICE4 engine, up to 120km DWDM", isTelecom: true },
  { pid: "INF-QSFP-DD-400G-ZRP",   formFactor: "QSFP-DD", speedGbps: 400, speed: "400G",  reachMeters: 2000000, reachLabel: "ZR+",       fiberType: "SMF", connector: "LC",  wavelengths: "C-band",      notes: "400G ZR+ OpenROADM coherent QSFP-DD for ICE4, up to 2000km long-haul", isTelecom: true },
  { pid: "INF-ICE4-QSFP-DD-400G",  formFactor: "QSFP-DD", speedGbps: 400, speed: "400G",  reachMeters: 4000000, reachLabel: "ICE4-Coh",  fiberType: "SMF", connector: "LC",  wavelengths: "C-band",      notes: "Infinera ICE4 coherent engine QSFP-DD 400G, up to 4000km transoceanic, FlexE", isTelecom: true },

  // ── Muxponder module (Telecom) ────────────────────────────────────────────
  { pid: "INF-MXPONDER-10x10G",    formFactor: "CFP2",    speedGbps: 100, speed: "100G",  reachMeters: 2000000, reachLabel: "MXP-10x10G",fiberType: "SMF", connector: "LC",  wavelengths: "C-band",      notes: "Groove G30 10x10G muxponder module; aggregates 10x10G client into 100G coherent line", isTelecom: true },
];

export async function scrapeInfineraGrooveOem(): Promise<void> {
  console.log("=== Infinera Groove/ICE OEM Transceiver Seed ===\n");

  const vendorId = await ensureVendor(
    "Infinera",
    "oem",
    "https://www.infinera.com",
    undefined
  );

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

  for (const p of INFINERA_GROOVE_PIDS) {
    const slug = `infinera-${p.pid.toLowerCase().replace(/[^a-z0-9]+/g, "-")}`;
    const category = p.isTelecom ? "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),
           is_oem_seed  = true,
           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=== Infinera Groove/ICE OEM Seed Complete ===`);
  console.log(`  Inserted: ${inserted}`);
  console.log(`  Updated:  ${updated}`);
  console.log(`  Errors:   ${errors}`);
  console.log(`  Total PIDs: ${INFINERA_GROOVE_PIDS.length}\n`);
}

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