/**
 * Foundry Networks (Legacy) OEM Transceiver Catalog Seed
 *
 * Foundry Networks (acquired by Brocade in 2008) made FastIron and NetIron
 * switches/routers. Their SFP/SFP+ PIDs are referenced in enterprise upgrades
 * and legacy BoMs. The E1MG/10G-SFPP family carries over from Foundry heritage.
 *
 * Sources:
 *  - Foundry FastIron switch hardware installation guide
 *  - Foundry NetIron CES/CER router SFP catalog
 *  - Brocade/Foundry compatibility matrices for SFP modules
 *
 * Run:  tsx packages/scraper/src/scrapers/foundry-networks-oem.ts
 * Cron: daily at 05:15
 */

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

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

const FOUNDRY_PIDS: FoundryPID[] = [
  // ── 100M/1G SFP — FastIron GigE ──────────────────────────────────────
  { pid: "FDR-SFP-FX",       formFactor: "SFP",   speedGbps: 0.1,speed: "100M", reachMeters: 2000,  reachLabel: "FX",   fiberType: "MMF", connector: "LC",  wavelengths: "1310nm", standard: "100BASE-FX",  notes: "Foundry 100M SFP FX 2km MM" },
  { pid: "FDR-SFP-LX10",     formFactor: "SFP",   speedGbps: 0.1,speed: "100M", reachMeters: 10000, reachLabel: "LX10", fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "100BASE-LX10",notes: "Foundry 100M SFP LX10 10km SM" },
  { pid: "FDR-E1MG-SX",      formFactor: "SFP",   speedGbps: 1,  speed: "1G",   reachMeters: 550,   reachLabel: "SX",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  standard: "1000BASE-SX", notes: "Foundry 1G SFP SX 550m — FastIron edge" },
  { pid: "FDR-E1MG-LX",      formFactor: "SFP",   speedGbps: 1,  speed: "1G",   reachMeters: 10000, reachLabel: "LX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "1000BASE-LX", notes: "Foundry 1G SFP LX 10km SM" },
  { pid: "FDR-E1MG-EX",      formFactor: "SFP",   speedGbps: 1,  speed: "1G",   reachMeters: 40000, reachLabel: "EX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "1000BASE-EX", notes: "Foundry 1G SFP EX 40km SM" },
  { pid: "FDR-E1MG-ZX",      formFactor: "SFP",   speedGbps: 1,  speed: "1G",   reachMeters: 80000, reachLabel: "ZX",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "1000BASE-ZX", notes: "Foundry 1G SFP ZX 80km SM" },
  { pid: "FDR-E1MG-TX",      formFactor: "SFP",   speedGbps: 1,  speed: "1G",   reachMeters: 100,   reachLabel: "T",    fiberType: "CU",  connector: "RJ-45",                       standard: "1000BASE-T",  notes: "Foundry 1G copper SFP RJ45" },
  // ── SFP+ 10G — NetIron CES/CER and FastIron SX/TurboIron 24X ─────────
  { pid: "FDR-10G-SFPP-SR",   formFactor: "SFP+",  speedGbps: 10, speed: "10G",  reachMeters: 300,   reachLabel: "SR",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  standard: "10GBASE-SR",  notes: "Foundry 10G SFP+ SR 300m MM" },
  { pid: "FDR-10G-SFPP-LR",   formFactor: "SFP+",  speedGbps: 10, speed: "10G",  reachMeters: 10000, reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "10GBASE-LR",  notes: "Foundry 10G SFP+ LR 10km SM" },
  { pid: "FDR-10G-SFPP-ER",   formFactor: "SFP+",  speedGbps: 10, speed: "10G",  reachMeters: 40000, reachLabel: "ER",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "10GBASE-ER",  notes: "Foundry 10G SFP+ ER 40km SM" },
  { pid: "FDR-10G-SFPP-ZR",   formFactor: "SFP+",  speedGbps: 10, speed: "10G",  reachMeters: 80000, reachLabel: "ZR",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "10GBASE-ZR",  notes: "Foundry 10G SFP+ ZR 80km SM" },
  // ── XFP 10G — NetIron XMR/MLX ────────────────────────────────────────
  { pid: "FDR-XFP-10G-SR",    formFactor: "XFP",   speedGbps: 10, speed: "10G",  reachMeters: 300,   reachLabel: "SR",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  standard: "10GBASE-SR",  notes: "Foundry 10G XFP SR 300m for NetIron" },
  { pid: "FDR-XFP-10G-LR",    formFactor: "XFP",   speedGbps: 10, speed: "10G",  reachMeters: 10000, reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "10GBASE-LR",  notes: "Foundry 10G XFP LR 10km SM" },
  { pid: "FDR-XFP-10G-ER",    formFactor: "XFP",   speedGbps: 10, speed: "10G",  reachMeters: 40000, reachLabel: "ER",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "10GBASE-ER",  notes: "Foundry 10G XFP ER 40km SM" },
  { pid: "FDR-XFP-10G-ZR",    formFactor: "XFP",   speedGbps: 10, speed: "10G",  reachMeters: 80000, reachLabel: "ZR",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "10GBASE-ZR",  notes: "Foundry 10G XFP ZR 80km SM" },
  // ── DAC ───────────────────────────────────────────────────────────────
  { pid: "FDR-DAC-SFP-1G-1M", formFactor: "SFP",   speedGbps: 1,  speed: "1G",   reachMeters: 1,     reachLabel: "DAC",  fiberType: "DAC", connector: "DAC",                        notes: "Foundry 1G SFP DAC twinax 1m" },
  { pid: "FDR-DAC-10G-1M",    formFactor: "SFP+",  speedGbps: 10, speed: "10G",  reachMeters: 1,     reachLabel: "DAC",  fiberType: "DAC", connector: "DAC",                        notes: "Foundry 10G SFP+ DAC twinax 1m" },
  { pid: "FDR-DAC-10G-3M",    formFactor: "SFP+",  speedGbps: 10, speed: "10G",  reachMeters: 3,     reachLabel: "DAC",  fiberType: "DAC", connector: "DAC",                        notes: "Foundry 10G SFP+ DAC twinax 3m" },
];

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

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

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

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

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