/**
 * Juniper MX OEM Transceiver Catalog Seed
 *
 * Seeds Juniper MX-branded transceiver PIDs for MX480, MX960, MX10003,
 * and PTX series service provider routers and packet-optical platforms,
 * including OC-n SONET, CFP2-DCO coherent, and 400G ZR modules.
 *
 * Sources:
 *  - Juniper MX Series Router Hardware Guide
 *  - Juniper PTX Series Packet Transport Router Data Sheet
 *  - Juniper Optics Compatibility Tool (HCT)
 *  - Juniper Networks Transceiver Module Reference (TN1157)
 *
 * Run:  tsx packages/scraper/src/scrapers/juniper-mx-oem.ts
 * Cron: daily at 13:15
 */

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

interface JuniperMxPID {
  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-3/12/48 SONET, CFP2-DCO coherent, ZR variants.
const JUNIPER_MX_PIDS: JuniperMxPID[] = [
  // ── 1G SFP ──────────────────────────────────────────────────────────────
  { pid: "SFP-1GE-SX",      formFactor: "SFP",    speedGbps: 1,    speed: "1G",    reachMeters: 550,     reachLabel: "SX",          fiberType: "MMF", connector: "LC",   wavelengths: "850nm",           standard: "1000BASE-SX",   category: "DataCenter", notes: "Juniper MX 1G multimode SFP" },
  { pid: "SFP-1GE-LX",      formFactor: "SFP",    speedGbps: 1,    speed: "1G",    reachMeters: 10000,   reachLabel: "LX",          fiberType: "SMF", connector: "LC",   wavelengths: "1310nm",          standard: "1000BASE-LX",   category: "DataCenter", notes: "Juniper MX 1G single-mode SFP 10km" },
  { pid: "SFP-1GE-T",       formFactor: "SFP",    speedGbps: 1,    speed: "1G",    reachMeters: 100,     reachLabel: "T",           fiberType: "DAC", connector: "RJ45",                                  standard: "1000BASE-T",    category: "DataCenter", notes: "Juniper MX 1G copper SFP" },

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

  // ── SONET/SDH OC-n SFP ──────────────────────────────────────────────────
  { pid: "SFP-OC3-MM",      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: "Juniper MX OC-3 multimode short-reach SONET SFP" },
  { pid: "SFP-OC3-SM10",    formFactor: "SFP",    speedGbps: 0.155, speed: "OC3",  reachMeters: 10000,   reachLabel: "LR-10km",     fiberType: "SMF", connector: "LC",   wavelengths: "1310nm",          standard: "OC-3/STM-1",    category: "Telecom",    notes: "Juniper MX OC-3 single-mode 10km SONET SFP" },
  { pid: "SFP-OC12-SM10",   formFactor: "SFP",    speedGbps: 0.622, speed: "OC12", reachMeters: 10000,   reachLabel: "LR-10km",     fiberType: "SMF", connector: "LC",   wavelengths: "1310nm",          standard: "OC-12/STM-4",   category: "Telecom",    notes: "Juniper MX OC-12 single-mode 10km SONET SFP" },
  { pid: "SFP-OC48-SM",     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: "Juniper MX OC-48 short-reach SONET SFP" },

  // ── 40G QSFP+ ───────────────────────────────────────────────────────────
  { pid: "QSFP-40G-SR4",    formFactor: "QSFP+",  speedGbps: 40,   speed: "40G",   reachMeters: 150,     reachLabel: "SR4",         fiberType: "MMF", connector: "MPO", wavelengths: "850nm",           standard: "40GBASE-SR4",   category: "DataCenter", notes: "Juniper MX 40G multimode QSFP+" },
  { pid: "QSFP-40G-LR4",    formFactor: "QSFP+",  speedGbps: 40,   speed: "40G",   reachMeters: 10000,   reachLabel: "LR4",         fiberType: "SMF", connector: "LC",  wavelengths: "1295-1310nm",     standard: "40GBASE-LR4",   category: "DataCenter", notes: "Juniper MX 40G single-mode QSFP+ 10km" },

  // ── 25G SFP28 ───────────────────────────────────────────────────────────
  { pid: "SFP28-25G-SR",    formFactor: "SFP28",  speedGbps: 25,   speed: "25G",   reachMeters: 100,     reachLabel: "SR",          fiberType: "MMF", connector: "LC",   wavelengths: "850nm",           standard: "25GBASE-SR",    category: "DataCenter", notes: "Juniper MX 25G multimode SFP28" },
  { pid: "SFP28-25G-LR",    formFactor: "SFP28",  speedGbps: 25,   speed: "25G",   reachMeters: 10000,   reachLabel: "LR",          fiberType: "SMF", connector: "LC",   wavelengths: "1310nm",          standard: "25GBASE-LR",    category: "DataCenter", notes: "Juniper MX 25G single-mode SFP28 10km" },

  // ── 100G QSFP28 ─────────────────────────────────────────────────────────
  { pid: "QSFP28-100G-SR4", formFactor: "QSFP28", speedGbps: 100,  speed: "100G",  reachMeters: 100,     reachLabel: "SR4",         fiberType: "MMF", connector: "MPO", wavelengths: "850nm",           standard: "100GBASE-SR4",  category: "DataCenter", notes: "Juniper MX 100G multimode QSFP28" },
  { pid: "QSFP28-100G-LR4", formFactor: "QSFP28", speedGbps: 100,  speed: "100G",  reachMeters: 10000,   reachLabel: "LR4",         fiberType: "SMF", connector: "LC",  wavelengths: "1295-1310nm",     standard: "100GBASE-LR4",  category: "DataCenter", notes: "Juniper MX 100G single-mode QSFP28 10km" },
  { pid: "QSFP28-100G-ER4", formFactor: "QSFP28", speedGbps: 100,  speed: "100G",  reachMeters: 40000,   reachLabel: "ER4",         fiberType: "SMF", connector: "LC",  wavelengths: "1295-1310nm",     standard: "100GBASE-ER4",  category: "DataCenter", notes: "Juniper MX 100G extended-reach QSFP28 40km" },

  // ── Coherent / PTX high-speed ────────────────────────────────────────────
  { 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: "Juniper PTX 200G CFP2 digital coherent optics long-haul DWDM" },
  { pid: "QSFP-DD-ZR-400G", 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: "Juniper PTX 400G QSFP-DD coherent ZR 120km metro DWDM" },
];

export async function scrapeJuniperMxOem(): Promise<void> {
  console.log("=== Juniper MX OEM Transceiver Seed ===\n");

  const vendorId = await ensureVendor(
    "Juniper MX",
    "oem",
    "https://www.juniper.net/us/en/products/routers/mx-series.html",
    undefined
  );

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

  for (const p of JUNIPER_MX_PIDS) {
    const slug = `juniper-mx-${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 = JUNIPER_MX_PIDS.filter(p => p.category === "Telecom").length;
  const dcCount      = JUNIPER_MX_PIDS.filter(p => p.category === "DataCenter").length;

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

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