/**
 * Brocade (Legacy) OEM Transceiver Catalog Seed
 *
 * Brocade Communications (acquired by Broadcom/Extreme Networks in 2017) made
 * FC SAN and Ethernet switches. Their ICX/FCX/VDX/MLX series are still widely
 * deployed. Brocade-branded SFPs remain in service and appear frequently in
 * legacy BoMs and upgrade requests.
 *
 * Sources:
 *  - Brocade ICX/FCX switch transceiver catalog
 *  - Brocade MLX/XMR router SFP/QSFP compatibility guide
 *  - Brocade VDX/FCoE SFP+ catalog
 *
 * Run:  tsx packages/scraper/src/scrapers/brocade-legacy-oem.ts
 * Cron: daily at 05:00
 */

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

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

const BROCADE_PIDS: BrocadePID[] = [
  // ── SFP 1G — ICX/FCX GigE ────────────────────────────────────────────
  { pid: "E1MG-SX",      formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 550,   reachLabel: "SX",   fiberType: "MMF", connector: "LC",   wavelengths: "850nm",  standard: "1000BASE-SX", notes: "Brocade 1G SFP SX 550m MM — ICX/FCX series" },
  { pid: "E1MG-LX",      formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 10000, reachLabel: "LX",   fiberType: "SMF", connector: "LC",   wavelengths: "1310nm", standard: "1000BASE-LX", notes: "Brocade 1G SFP LX 10km SM" },
  { pid: "E1MG-EX",      formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 40000, reachLabel: "EX",   fiberType: "SMF", connector: "LC",   wavelengths: "1310nm", standard: "1000BASE-EX", notes: "Brocade 1G SFP EX 40km SM" },
  { pid: "E1MG-ZX",      formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 80000, reachLabel: "ZX",   fiberType: "SMF", connector: "LC",   wavelengths: "1550nm", standard: "1000BASE-ZX", notes: "Brocade 1G SFP ZX 80km SM" },
  { pid: "E1MG-TX",      formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 100,   reachLabel: "T",    fiberType: "CU",  connector: "RJ-45",                       standard: "1000BASE-T",  notes: "Brocade 1G copper SFP RJ45" },
  { pid: "E1MG-LHA",     formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 70000, reachLabel: "LHA",  fiberType: "SMF", connector: "LC",   wavelengths: "1550nm", notes: "Brocade 1G SFP LH 70km SM" },
  // ── SFP 1G BiDi — ICX uplink point-to-point ──────────────────────────
  { pid: "E1MG-BXU-A",   formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 10000, reachLabel: "BX-U", fiberType: "SMF", connector: "LC",   wavelengths: "1310nm TX / 1490nm RX", notes: "Brocade 1G BiDi SFP BX-U 1310/1490" },
  { pid: "E1MG-BXD-A",   formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 10000, reachLabel: "BX-D", fiberType: "SMF", connector: "LC",   wavelengths: "1490nm TX / 1310nm RX", notes: "Brocade 1G BiDi SFP BX-D 1490/1310" },
  // ── SFP+ 10G — ICX-7750/FCX/VDX ─────────────────────────────────────
  { pid: "10G-SFPP-SR",  formFactor: "SFP+",  speedGbps: 10, speed: "10G", reachMeters: 300,   reachLabel: "SR",   fiberType: "MMF", connector: "LC",   wavelengths: "850nm",  standard: "10GBASE-SR",  notes: "Brocade 10G SFP+ SR 300m" },
  { pid: "10G-SFPP-LR",  formFactor: "SFP+",  speedGbps: 10, speed: "10G", reachMeters: 10000, reachLabel: "LR",   fiberType: "SMF", connector: "LC",   wavelengths: "1310nm", standard: "10GBASE-LR",  notes: "Brocade 10G SFP+ LR 10km" },
  { pid: "10G-SFPP-ER",  formFactor: "SFP+",  speedGbps: 10, speed: "10G", reachMeters: 40000, reachLabel: "ER",   fiberType: "SMF", connector: "LC",   wavelengths: "1550nm", standard: "10GBASE-ER",  notes: "Brocade 10G SFP+ ER 40km" },
  { pid: "10G-SFPP-ZR",  formFactor: "SFP+",  speedGbps: 10, speed: "10G", reachMeters: 80000, reachLabel: "ZR",   fiberType: "SMF", connector: "LC",   wavelengths: "1550nm", standard: "10GBASE-ZR",  notes: "Brocade 10G SFP+ ZR 80km" },
  { pid: "10G-SFPP-BXU", formFactor: "SFP+",  speedGbps: 10, speed: "10G", reachMeters: 10000, reachLabel: "BX-U", fiberType: "SMF", connector: "LC",   wavelengths: "1270nm TX / 1330nm RX", notes: "Brocade 10G BiDi SFP+ BX-U" },
  { pid: "10G-SFPP-BXD", formFactor: "SFP+",  speedGbps: 10, speed: "10G", reachMeters: 10000, reachLabel: "BX-D", fiberType: "SMF", connector: "LC",   wavelengths: "1330nm TX / 1270nm RX", notes: "Brocade 10G BiDi SFP+ BX-D" },
  // ── QSFP+ 40G — MLX/XMR router uplinks ──────────────────────────────
  { pid: "40G-QSFP-SR4",  formFactor: "QSFP+", speedGbps: 40, speed: "40G", reachMeters: 150,  reachLabel: "SR4",  fiberType: "MMF", connector: "MPO",  wavelengths: "850nm",  standard: "40GBASE-SR4", notes: "Brocade 40G QSFP+ SR4 150m MPO" },
  { pid: "40G-QSFP-LR4",  formFactor: "QSFP+", speedGbps: 40, speed: "40G", reachMeters: 10000,reachLabel: "LR4",  fiberType: "SMF", connector: "LC",   wavelengths: "1310nm", standard: "40GBASE-LR4", notes: "Brocade 40G QSFP+ LR4 10km LC" },
  // ── DAC ───────────────────────────────────────────────────────────────
  { pid: "10G-SFPP-TWX-0101",formFactor:"SFP+", speedGbps: 10, speed: "10G", reachMeters: 1,   reachLabel: "DAC",  fiberType: "DAC", connector: "DAC",                         notes: "Brocade 10G SFP+ twinax DAC 1m" },
  { pid: "10G-SFPP-TWX-0301",formFactor:"SFP+", speedGbps: 10, speed: "10G", reachMeters: 3,   reachLabel: "DAC",  fiberType: "DAC", connector: "DAC",                         notes: "Brocade 10G SFP+ twinax DAC 3m" },
];

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

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

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

  for (const p of BROCADE_PIDS) {
    const slug = `brocade-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,true,$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=== Brocade (Legacy) OEM Seed Complete ===`);
  console.log(`  Inserted: ${inserted}, Updated: ${updated}, Errors: ${errors}`);
  console.log(`  Total PIDs: ${BROCADE_PIDS.length}\n`);
}

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