/**
 * Nortel Networks (Legacy) OEM Transceiver Catalog Seed
 *
 * Nortel Networks (bankrupt 2009, assets acquired by Ericsson, Avaya, Ciena,
 * etc.) made OPTera, Passport, BayStack, and Accelar switches/routers.
 * Their SFP PIDs (AA1419xxx series) still appear in deployed gear and
 * network audits for legacy campus/carrier networks.
 *
 * Sources:
 *  - Nortel Passport 8600/8300 hardware installation guide
 *  - Nortel BayStack 5500/4500 optics compatibility matrix
 *  - Nortel OPTera Connect SFP catalog
 *
 * Run:  tsx packages/scraper/src/scrapers/nortel-legacy-oem.ts
 * Cron: daily at 05:45
 */

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

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

const NORTEL_PIDS: NortelPID[] = [
  // ── 100M SFP — BayStack 425/470 ──────────────────────────────────────
  { pid: "AA1419015-E5",  formFactor: "SFP",   speedGbps: 0.1, speed: "100M", reachMeters: 2000,  reachLabel: "FX",   fiberType: "MMF", connector: "LC",  wavelengths: "1310nm", standard: "100BASE-FX",  notes: "Nortel 100M SFP FX 2km MM — BayStack" },
  { pid: "AA1419043-E5",  formFactor: "SFP",   speedGbps: 0.1, speed: "100M", reachMeters: 15000, reachLabel: "LX10", fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "100BASE-LX10",notes: "Nortel 100M SFP LX10 15km SM" },
  // ── 1G SFP — Passport 8600/8300, BayStack 5500/4500 ──────────────────
  { pid: "AA1419001-E5",  formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 550,   reachLabel: "SX",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  standard: "1000BASE-SX", notes: "Nortel 1G SFP SX 550m MM — Passport 8600" },
  { pid: "AA1419002-E5",  formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 10000, reachLabel: "LX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "1000BASE-LX", notes: "Nortel 1G SFP LX 10km SM" },
  { pid: "AA1419003-E5",  formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 40000, reachLabel: "EX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "1000BASE-EX", notes: "Nortel 1G SFP EX 40km SM" },
  { pid: "AA1419004-E5",  formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 80000, reachLabel: "ZX",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "1000BASE-ZX", notes: "Nortel 1G SFP ZX 80km SM" },
  { pid: "AA1419005-E5",  formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 100,   reachLabel: "T",    fiberType: "CU",  connector: "RJ-45",                       standard: "1000BASE-T",  notes: "Nortel 1G copper SFP RJ45" },
  { pid: "AA1419010-E5",  formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 120000,reachLabel: "LH",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", notes: "Nortel 1G SFP LH 120km SM extended" },
  // ── 1G SFP BiDi — Passport ───────────────────────────────────────────
  { pid: "AA1419039-E5",  formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 10000, reachLabel: "BX-U", fiberType: "SMF", connector: "LC",  wavelengths: "1310nm TX / 1490nm RX", notes: "Nortel 1G BiDi SFP BX-U" },
  { pid: "AA1419040-E5",  formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 10000, reachLabel: "BX-D", fiberType: "SMF", connector: "LC",  wavelengths: "1490nm TX / 1310nm RX", notes: "Nortel 1G BiDi SFP BX-D" },
  // ── 10G XFP — OPTera Connect HDX / Passport 8810 ─────────────────────
  { pid: "AA1403001-E6",  formFactor: "XFP",   speedGbps: 10,  speed: "10G",  reachMeters: 300,   reachLabel: "SR",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  standard: "10GBASE-SR",  notes: "Nortel 10G XFP SR 300m MM — OPTera/Passport 8810" },
  { pid: "AA1403002-E6",  formFactor: "XFP",   speedGbps: 10,  speed: "10G",  reachMeters: 10000, reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "10GBASE-LR",  notes: "Nortel 10G XFP LR 10km SM" },
  { pid: "AA1403003-E6",  formFactor: "XFP",   speedGbps: 10,  speed: "10G",  reachMeters: 40000, reachLabel: "ER",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "10GBASE-ER",  notes: "Nortel 10G XFP ER 40km SM" },
  { pid: "AA1403004-E6",  formFactor: "XFP",   speedGbps: 10,  speed: "10G",  reachMeters: 80000, reachLabel: "ZR",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "10GBASE-ZR",  notes: "Nortel 10G XFP ZR 80km SM" },
  // ── 10G SFP+ — Passport 8832/ERS series (late Nortel era) ────────────
  { pid: "AA1419049-E5",  formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 300,   reachLabel: "SR",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  standard: "10GBASE-SR",  notes: "Nortel 10G SFP+ SR 300m — ERS 8800" },
  { pid: "AA1419050-E5",  formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 10000, reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "10GBASE-LR",  notes: "Nortel 10G SFP+ LR 10km SM" },
  // ── GBIC legacy — Accelar 1100/1200 ──────────────────────────────────
  { pid: "AA1419013-E5",  formFactor: "GBIC",  speedGbps: 1,   speed: "1G",   reachMeters: 550,   reachLabel: "SX",   fiberType: "MMF", connector: "SC",  wavelengths: "850nm",  standard: "1000BASE-SX", notes: "Nortel 1G GBIC SX 550m SC MM — Accelar series" },
  { pid: "AA1419014-E5",  formFactor: "GBIC",  speedGbps: 1,   speed: "1G",   reachMeters: 10000, reachLabel: "LX",   fiberType: "SMF", connector: "SC",  wavelengths: "1310nm", standard: "1000BASE-LX", notes: "Nortel 1G GBIC LX 10km SC SM" },
];

export async function scrapeNortelLegacyOem(): Promise<void> {
  console.log("=== Nortel Networks (Legacy) OEM Transceiver Seed ===\n");

  const vendorId = await ensureVendor(
    "Nortel Networks",
    "oem",
    "https://www.broadcom.com",
    undefined
  );

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

  for (const p of NORTEL_PIDS) {
    const slug = `nortel-legacy-${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,false,$12,'Legacy','DataCenter',$13)
         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, 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=== Nortel Networks (Legacy) OEM Seed Complete ===`);
  console.log(`  Inserted: ${inserted}, Updated: ${updated}, Errors: ${errors}`);
  console.log(`  Total PIDs: ${NORTEL_PIDS.length}\n`);
}

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