/**
 * Cisco ASR OEM Transceiver Catalog Seed
 *
 * Seeds Cisco ASR-branded transceiver PIDs for ASR 1000, 9000, and 9900
 * series service provider edge and core routers, including OC-n SONET,
 * CFP/CFP2 coherent, and DWDM modules.
 *
 * Sources:
 *  - Cisco ASR 9000 Series Router Data Sheet
 *  - Cisco ASR 1000 Series Transceiver Module Selection Guide
 *  - Cisco 400G Coherent Pluggable Optics for ASR 9000 Data Sheet
 *  - Cisco DWDM SFP10G Transceiver Module (OID EDCS-959978)
 *
 * Run:  tsx packages/scraper/src/scrapers/cisco-asr-oem.ts
 * Cron: daily at 13:00
 */

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

interface AsrPID {
  pid: string;
  formFactor: string;
  speedGbps: number;
  speed: string;
  reachMeters: number;
  reachLabel: string;
  fiberType: string;
  connector: string;
  wavelengths?: string;
  standard?: string;
  category: "DataCenter" | "Telecom";
  notes?: string;
}

// Telecom PIDs: OC-n SONET, CFP, CFP2-DCO coherent, DWDM, ZR variants.
const ASR_PIDS: AsrPID[] = [
  // ── GE SFP ──────────────────────────────────────────────────────────────
  { pid: "SFP-GE-S",        formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 550,    reachLabel: "SX",        fiberType: "MMF", connector: "LC",   wavelengths: "850nm",           standard: "1000BASE-SX",   category: "DataCenter", notes: "Cisco ASR GE multimode SFP" },

  // ── 10G SFP+ ────────────────────────────────────────────────────────────
  { pid: "SFP-10G-SR",      formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 300,    reachLabel: "SR",        fiberType: "MMF", connector: "LC",   wavelengths: "850nm",           standard: "10GBASE-SR",    category: "DataCenter", notes: "Cisco ASR 10G multimode SFP+" },
  { pid: "SFP-10G-LR",      formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 10000,  reachLabel: "LR",        fiberType: "SMF", connector: "LC",   wavelengths: "1310nm",          standard: "10GBASE-LR",    category: "DataCenter", notes: "Cisco ASR 10G single-mode SFP+ 10km" },
  { pid: "SFP-10G-LR-S",    formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 10000,  reachLabel: "LR",        fiberType: "SMF", connector: "LC",   wavelengths: "1310nm",          standard: "10GBASE-LR",    category: "DataCenter", notes: "Cisco ASR 10G single-mode SFP+ S-variant" },
  { pid: "SFP-10G-ER",      formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 40000,  reachLabel: "ER",        fiberType: "SMF", connector: "LC",   wavelengths: "1550nm",          standard: "10GBASE-ER",    category: "DataCenter", notes: "Cisco ASR 10G extended-reach SFP+ 40km" },
  { pid: "SFP-10G-ZR",      formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 80000,  reachLabel: "ZR",        fiberType: "SMF", connector: "LC",   wavelengths: "1550nm",          standard: "10GBASE-ZR",    category: "Telecom",    notes: "Cisco ASR 10G ZR SFP+ 80km metro/SP" },

  // ── SONET/SDH OC-n SFP ──────────────────────────────────────────────────
  { pid: "SFP-OC3-SR",      formFactor: "SFP",   speedGbps: 0.155, speed: "OC3",  reachMeters: 2000,  reachLabel: "SR",       fiberType: "MMF", connector: "LC",   wavelengths: "1310nm",          standard: "OC-3/STM-1",    category: "Telecom",    notes: "Cisco ASR OC-3 short-reach SONET SFP" },
  { pid: "SFP-OC3-LR",      formFactor: "SFP",   speedGbps: 0.155, speed: "OC3",  reachMeters: 40000, reachLabel: "LR",       fiberType: "SMF", connector: "LC",   wavelengths: "1310nm",          standard: "OC-3/STM-1",    category: "Telecom",    notes: "Cisco ASR OC-3 long-reach SONET SFP" },
  { pid: "SFP-OC12-SR",     formFactor: "SFP",   speedGbps: 0.622, speed: "OC12", reachMeters: 2000,  reachLabel: "SR",       fiberType: "MMF", connector: "LC",   wavelengths: "1310nm",          standard: "OC-12/STM-4",   category: "Telecom",    notes: "Cisco ASR OC-12 short-reach SONET SFP" },
  { pid: "SFP-OC12-LR",     formFactor: "SFP",   speedGbps: 0.622, speed: "OC12", reachMeters: 40000, reachLabel: "LR",       fiberType: "SMF", connector: "LC",   wavelengths: "1310nm",          standard: "OC-12/STM-4",   category: "Telecom",    notes: "Cisco ASR OC-12 long-reach SONET SFP" },
  { pid: "SFP-OC48-SR",     formFactor: "SFP",   speedGbps: 2.488, speed: "OC48", reachMeters: 2000,  reachLabel: "SR",       fiberType: "MMF", connector: "LC",   wavelengths: "1310nm",          standard: "OC-48/STM-16",  category: "Telecom",    notes: "Cisco ASR OC-48 short-reach SONET SFP" },
  { pid: "SFP-OC48-LR",     formFactor: "SFP",   speedGbps: 2.488, speed: "OC48", reachMeters: 40000, reachLabel: "LR",       fiberType: "SMF", connector: "LC",   wavelengths: "1310nm",          standard: "OC-48/STM-16",  category: "Telecom",    notes: "Cisco ASR OC-48 long-reach SONET SFP" },

  // ── 40G QSFP+ ───────────────────────────────────────────────────────────
  { pid: "QSFP-40G-LR4-S",  formFactor: "QSFP+",  speedGbps: 40,  speed: "40G",  reachMeters: 10000, reachLabel: "LR4",       fiberType: "SMF", connector: "LC",   wavelengths: "1295-1310nm",     standard: "40GBASE-LR4",   category: "DataCenter", notes: "Cisco ASR 40G QSFP+ LR4 10km" },

  // ── 100G QSFP28 / CFP ───────────────────────────────────────────────────
  { pid: "QSFP-100G-LR4-S",  formFactor: "QSFP28",  speedGbps: 100, speed: "100G", reachMeters: 10000, reachLabel: "LR4",      fiberType: "SMF", connector: "LC",   wavelengths: "1295-1310nm",     standard: "100GBASE-LR4",  category: "DataCenter", notes: "Cisco ASR 100G QSFP28 LR4 10km" },
  { pid: "QSFP-100G-ZR-S",   formFactor: "QSFP28",  speedGbps: 100, speed: "100G", reachMeters: 80000, reachLabel: "ZR",       fiberType: "SMF", connector: "LC",   wavelengths: "1550nm",          standard: "100GBASE-ZR",   category: "Telecom",    notes: "Cisco ASR 100G coherent ZR QSFP28 80km metro" },
  { pid: "CFP-100G-LR4",     formFactor: "CFP",     speedGbps: 100, speed: "100G", reachMeters: 10000, reachLabel: "LR4",      fiberType: "SMF", connector: "LC",   wavelengths: "1295-1310nm",     standard: "100GBASE-LR4",  category: "Telecom",    notes: "Cisco ASR 100G CFP LR4 10km SP router line card" },
  { pid: "CFP2-100G-LR4",    formFactor: "CFP2",    speedGbps: 100, speed: "100G", reachMeters: 10000, reachLabel: "LR4",      fiberType: "SMF", connector: "LC",   wavelengths: "1295-1310nm",     standard: "100GBASE-LR4",  category: "Telecom",    notes: "Cisco ASR 100G CFP2 LR4 10km compact form factor" },
  { pid: "CFP2-DCO-200G",    formFactor: "CFP2",    speedGbps: 200, speed: "200G", reachMeters: 1000000, reachLabel: "DCO-longhaul", fiberType: "SMF", connector: "LC", wavelengths: "C-band DWDM",  standard: "OTN/FlexE",     category: "Telecom",    notes: "Cisco ASR 200G CFP2 digital coherent optics long-haul DWDM" },

  // ── DWDM SFP10G / 400G ZR ───────────────────────────────────────────────
  { pid: "DWDM-SFP10G-C",    formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 80000,   reachLabel: "DWDM-C",   fiberType: "SMF", connector: "LC",   wavelengths: "C-band ITU 100GHz", standard: "10GBASE-ZR",  category: "Telecom",    notes: "Cisco ASR 9000 DWDM SFP10G C-band ITU tunable 80km" },
  { pid: "QSFP-DD-400G-ZR-S", formFactor: "QSFP-DD", speedGbps: 400, speed: "400G", reachMeters: 120000, reachLabel: "ZR-120km", fiberType: "SMF", connector: "LC", wavelengths: "C-band DWDM",     standard: "400ZR OpenROADM", category: "Telecom",  notes: "Cisco ASR 400G QSFP-DD coherent ZR 120km metro DWDM" },
];

export async function scrapeCiscoAsrOem(): Promise<void> {
  console.log("=== Cisco ASR OEM Transceiver Seed ===\n");

  const vendorId = await ensureVendor(
    "Cisco ASR",
    "oem",
    "https://www.cisco.com/c/en/us/products/routers/asr-series/index.html",
    undefined
  );

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

  for (const p of ASR_PIDS) {
    const slug = `cisco-asr-${p.pid.toLowerCase().replace(/[^a-z0-9]+/g, "-")}`;
    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),
           category     = EXCLUDED.category,
           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, p.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++;
    }
  }

  const telecomCount  = ASR_PIDS.filter(p => p.category === "Telecom").length;
  const dcCount       = ASR_PIDS.filter(p => p.category === "DataCenter").length;

  console.log(`\n=== Cisco ASR OEM Seed Complete ===`);
  console.log(`  Inserted: ${inserted}`);
  console.log(`  Updated:  ${updated}`);
  console.log(`  Errors:   ${errors}`);
  console.log(`  Total PIDs: ${ASR_PIDS.length} (Telecom: ${telecomCount}, DataCenter: ${dcCount})\n`);
}

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