/**
 * Extreme Networks (Legacy/Pre-2017) OEM Transceiver Catalog Seed
 *
 * Extreme Networks original portfolio (pre-Brocade ICX acquisition 2017)
 * covered Summit, BlackDiamond, and Alpine switch families. SFP modules
 * used the 10052H/10053H/10058H naming pattern; SFP+ used 10318/10319.
 * These PIDs are widely referenced in legacy enterprise BoMs.
 *
 * Sources:
 *  - Extreme Networks Summit/BlackDiamond optics compatibility guide
 *  - Extreme Networks module transceivers catalog (legacy)
 *  - ExtremeXOS hardware installation guide — optics appendix
 *
 * Run:  tsx packages/scraper/src/scrapers/extreme-legacy-oem.ts
 * Cron: daily at 05:30
 */

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

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

const EXTREME_PIDS: ExtremePID[] = [
  // ── 1G SFP — Summit/BlackDiamond 10052H/10053H series ────────────────
  { pid: "10052H",      formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 550,   reachLabel: "SX",  fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  standard: "1000BASE-SX", notes: "Extreme Networks 1G SFP SX 550m — Summit series" },
  { pid: "10053H",      formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 10000, reachLabel: "LX",  fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "1000BASE-LX", notes: "Extreme Networks 1G SFP LX 10km SM" },
  { pid: "10058H",      formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 40000, reachLabel: "EX",  fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "1000BASE-EX", notes: "Extreme Networks 1G SFP EX 40km SM" },
  { pid: "10059H",      formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 80000, reachLabel: "ZX",  fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "1000BASE-ZX", notes: "Extreme Networks 1G SFP ZX 80km SM" },
  { pid: "10070H",      formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 100,   reachLabel: "T",   fiberType: "CU",  connector: "RJ-45",                       standard: "1000BASE-T",  notes: "Extreme Networks 1G copper SFP RJ45" },
  { pid: "10056H",      formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 70000, reachLabel: "LH",  fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", notes: "Extreme Networks 1G SFP LH 70km SM" },
  // ── 1G SFP BiDi ──────────────────────────────────────────────────────
  { pid: "10061H",      formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 10000, reachLabel: "BX-U",fiberType: "SMF", connector: "LC",  wavelengths: "1310nm TX / 1490nm RX", notes: "Extreme Networks 1G BiDi SFP BX-U" },
  { pid: "10062H",      formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 10000, reachLabel: "BX-D",fiberType: "SMF", connector: "LC",  wavelengths: "1490nm TX / 1310nm RX", notes: "Extreme Networks 1G BiDi SFP BX-D" },
  // ── 10G SFP+ — Summit X440/X460/X670/BlackDiamond X8 ─────────────────
  { pid: "10318",       formFactor: "SFP+",  speedGbps: 10, speed: "10G", reachMeters: 300,   reachLabel: "SR",  fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  standard: "10GBASE-SR",  notes: "Extreme Networks 10G SFP+ SR 300m — Summit X-series" },
  { pid: "10319",       formFactor: "SFP+",  speedGbps: 10, speed: "10G", reachMeters: 10000, reachLabel: "LR",  fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "10GBASE-LR",  notes: "Extreme Networks 10G SFP+ LR 10km SM" },
  { pid: "10320",       formFactor: "SFP+",  speedGbps: 10, speed: "10G", reachMeters: 40000, reachLabel: "ER",  fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "10GBASE-ER",  notes: "Extreme Networks 10G SFP+ ER 40km SM" },
  { pid: "10321",       formFactor: "SFP+",  speedGbps: 10, speed: "10G", reachMeters: 80000, reachLabel: "ZR",  fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "10GBASE-ZR",  notes: "Extreme Networks 10G SFP+ ZR 80km SM" },
  { pid: "10322",       formFactor: "SFP+",  speedGbps: 10, speed: "10G", reachMeters: 10000, reachLabel: "BX-U",fiberType: "SMF", connector: "LC",  wavelengths: "1270nm TX / 1330nm RX", notes: "Extreme Networks 10G BiDi SFP+ BX-U" },
  { pid: "10323",       formFactor: "SFP+",  speedGbps: 10, speed: "10G", reachMeters: 10000, reachLabel: "BX-D",fiberType: "SMF", connector: "LC",  wavelengths: "1330nm TX / 1270nm RX", notes: "Extreme Networks 10G BiDi SFP+ BX-D" },
  // ── QSFP+ 40G — BlackDiamond X8/Summit X670 ──────────────────────────
  { pid: "10325",       formFactor: "QSFP+", speedGbps: 40, speed: "40G", reachMeters: 150,   reachLabel: "SR4", fiberType: "MMF", connector: "MPO", wavelengths: "850nm",  standard: "40GBASE-SR4", notes: "Extreme Networks 40G QSFP+ SR4 150m MPO" },
  { pid: "10326",       formFactor: "QSFP+", speedGbps: 40, speed: "40G", reachMeters: 10000, reachLabel: "LR4", fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "40GBASE-LR4", notes: "Extreme Networks 40G QSFP+ LR4 10km LC" },
  // ── DAC ───────────────────────────────────────────────────────────────
  { pid: "10302",       formFactor: "SFP+",  speedGbps: 10, speed: "10G", reachMeters: 1,     reachLabel: "DAC", fiberType: "DAC", connector: "DAC",                        notes: "Extreme Networks 10G SFP+ DAC 1m" },
  { pid: "10303",       formFactor: "SFP+",  speedGbps: 10, speed: "10G", reachMeters: 3,     reachLabel: "DAC", fiberType: "DAC", connector: "DAC",                        notes: "Extreme Networks 10G SFP+ DAC 3m" },
];

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

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

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

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

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