/**
 * Extreme Campus OEM Transceiver Catalog Seed
 *
 * Seeds Extreme Networks campus-branded transceiver PIDs for EXOS and
 * VOSS platforms including X440-G2, X450-G2, X460-G2, X620, X690,
 * X870, and VSP series switches.
 *
 * Sources:
 *  - Extreme Networks Optics Compatibility Matrix
 *  - Extreme Networks Transceiver Module Data Sheet
 *  - Extreme Networks Summit Switch Optics Selection Guide
 *  - ExtremeXOS Hardware Compatibility Table (EXOS HCT)
 *
 * Run:  tsx packages/scraper/src/scrapers/extreme-campus-oem.ts
 * Cron: daily at 14:00
 */

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

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

const EXTREME_CAMPUS_PIDS: ExtremeCampusPID[] = [
  // ── 1G SFP ──────────────────────────────────────────────────────────────
  { pid: "10301",    formFactor: "SFP",    speedGbps: 1,    speed: "1G",    reachMeters: 550,    reachLabel: "SX",      fiberType: "MMF", connector: "LC",   wavelengths: "850nm",       standard: "1000BASE-SX",   notes: "Extreme Networks 1GbE SX SFP 550m MMF (PN 10301)" },
  { pid: "10302",    formFactor: "SFP",    speedGbps: 1,    speed: "1G",    reachMeters: 10000,  reachLabel: "LX",      fiberType: "SMF", connector: "LC",   wavelengths: "1310nm",      standard: "1000BASE-LX",   notes: "Extreme Networks 1GbE LX SFP 10km SMF (PN 10302)" },
  { pid: "10303",    formFactor: "SFP",    speedGbps: 1,    speed: "1G",    reachMeters: 100,    reachLabel: "T",       fiberType: "DAC", connector: "RJ45",                              standard: "1000BASE-T",    notes: "Extreme Networks 1GbE copper SFP RJ45 (PN 10303)" },
  { pid: "10303T",   formFactor: "SFP",    speedGbps: 1,    speed: "1G",    reachMeters: 100,    reachLabel: "T",       fiberType: "DAC", connector: "RJ45",                              standard: "1000BASE-T",    notes: "Extreme Networks 1GbE copper SFP RJ45 alt SKU (PN 10303T)" },
  { pid: "10304",    formFactor: "SFP",    speedGbps: 1,    speed: "1G",    reachMeters: 70000,  reachLabel: "ZX-70km", fiberType: "SMF", connector: "LC",   wavelengths: "1550nm",      standard: "1000BASE-ZX",   notes: "Extreme Networks 1GbE ZX SFP 70km SMF (PN 10304)" },

  // ── 10G SFP+ ────────────────────────────────────────────────────────────
  { pid: "10305",    formFactor: "SFP+",   speedGbps: 10,   speed: "10G",   reachMeters: 300,    reachLabel: "SR",      fiberType: "MMF", connector: "LC",   wavelengths: "850nm",       standard: "10GBASE-SR",    notes: "Extreme Networks 10GbE SR SFP+ 300m MMF (PN 10305)" },
  { pid: "10306",    formFactor: "SFP+",   speedGbps: 10,   speed: "10G",   reachMeters: 10000,  reachLabel: "LR",      fiberType: "SMF", connector: "LC",   wavelengths: "1310nm",      standard: "10GBASE-LR",    notes: "Extreme Networks 10GbE LR SFP+ 10km SMF (PN 10306)" },
  { pid: "10307",    formFactor: "SFP+",   speedGbps: 10,   speed: "10G",   reachMeters: 40000,  reachLabel: "ER",      fiberType: "SMF", connector: "LC",   wavelengths: "1550nm",      standard: "10GBASE-ER",    notes: "Extreme Networks 10GbE ER SFP+ 40km SMF (PN 10307)" },
  { pid: "10308",    formFactor: "SFP+",   speedGbps: 10,   speed: "10G",   reachMeters: 80000,  reachLabel: "ZR",      fiberType: "SMF", connector: "LC",   wavelengths: "1550nm",      standard: "10GBASE-ZR",    notes: "Extreme Networks 10GbE ZR SFP+ 80km SMF (PN 10308)" },
  { pid: "10309",    formFactor: "SFP+",   speedGbps: 10,   speed: "10G",   reachMeters: 220,    reachLabel: "LRM",     fiberType: "MMF", connector: "LC",   wavelengths: "1310nm",      standard: "10GBASE-LRM",   notes: "Extreme Networks 10GbE LRM SFP+ 220m MMF (PN 10309)" },

  // ── 40G QSFP+ ───────────────────────────────────────────────────────────
  { pid: "10321",    formFactor: "QSFP+",  speedGbps: 40,   speed: "40G",   reachMeters: 150,    reachLabel: "SR4",     fiberType: "MMF", connector: "MPO", wavelengths: "850nm",        standard: "40GBASE-SR4",   notes: "Extreme Networks 40GbE SR4 QSFP+ 150m MMF (PN 10321)" },
  { pid: "10322",    formFactor: "QSFP+",  speedGbps: 40,   speed: "40G",   reachMeters: 10000,  reachLabel: "LR4",     fiberType: "SMF", connector: "LC",  wavelengths: "1295-1310nm",  standard: "40GBASE-LR4",   notes: "Extreme Networks 40GbE LR4 QSFP+ 10km SMF (PN 10322)" },

  // ── 25G SFP28 ───────────────────────────────────────────────────────────
  { pid: "10323",    formFactor: "SFP28",  speedGbps: 25,   speed: "25G",   reachMeters: 100,    reachLabel: "SR",      fiberType: "MMF", connector: "LC",   wavelengths: "850nm",       standard: "25GBASE-SR",    notes: "Extreme Networks 25GbE SR SFP28 100m MMF (PN 10323)" },
  { pid: "10324",    formFactor: "SFP28",  speedGbps: 25,   speed: "25G",   reachMeters: 10000,  reachLabel: "LR",      fiberType: "SMF", connector: "LC",   wavelengths: "1310nm",      standard: "25GBASE-LR",    notes: "Extreme Networks 25GbE LR SFP28 10km SMF (PN 10324)" },

  // ── 100G QSFP28 ─────────────────────────────────────────────────────────
  { pid: "10331",    formFactor: "QSFP28", speedGbps: 100,  speed: "100G",  reachMeters: 100,    reachLabel: "SR4",     fiberType: "MMF", connector: "MPO", wavelengths: "850nm",        standard: "100GBASE-SR4",  notes: "Extreme Networks 100GbE SR4 QSFP28 100m MMF (PN 10331)" },
  { pid: "10332",    formFactor: "QSFP28", speedGbps: 100,  speed: "100G",  reachMeters: 10000,  reachLabel: "LR4",     fiberType: "SMF", connector: "LC",  wavelengths: "1295-1310nm",  standard: "100GBASE-LR4",  notes: "Extreme Networks 100GbE LR4 QSFP28 10km SMF (PN 10332)" },

  // ── 400G QSFP-DD ────────────────────────────────────────────────────────
  { pid: "10340",    formFactor: "QSFP-DD", speedGbps: 400, speed: "400G",  reachMeters: 500,    reachLabel: "DR4",     fiberType: "SMF", connector: "MPO", wavelengths: "1310nm",       standard: "400GBASE-DR4",  notes: "Extreme Networks 400GbE DR4 QSFP-DD 500m SMF (PN 10340)" },
  { pid: "10341",    formFactor: "QSFP-DD", speedGbps: 400, speed: "400G",  reachMeters: 100,    reachLabel: "SR8",     fiberType: "MMF", connector: "MPO", wavelengths: "850nm",        standard: "400GBASE-SR8",  notes: "Extreme Networks 400GbE SR8 QSFP-DD 100m MMF (PN 10341)" },
];

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

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

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

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

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