/**
 * Avaya (Legacy) OEM Transceiver Catalog Seed
 *
 * Avaya acquired Nortel's enterprise networking business (ERS switches) in 2009.
 * Their ERS 3500/4500/5900/8x00 series and VSP 7200/8200/9000 remain in
 * enterprise campus networks. Avaya SFP PIDs follow the AA1419xxx (inherited
 * from Nortel) and newer 700480xxx numbering.
 *
 * Sources:
 *  - Avaya ERS 5900/4900 hardware installation guide
 *  - Avaya VSP 7200/8200 SFP compatibility guide
 *  - Avaya optics/transceivers catalog 2016
 *
 * Run:  tsx packages/scraper/src/scrapers/avaya-legacy-oem.ts
 * Cron: daily at 06:15
 */

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

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

const AVAYA_PIDS: AvayaPID[] = [
  // ── 1G SFP — ERS 3500/4500/5900 (AA1419 inherited from Nortel) ────────
  { pid: "AA1419001-E6",   formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 550,   reachLabel: "SX",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  standard: "1000BASE-SX", notes: "Avaya 1G SFP SX 550m MM — ERS series (Nortel heritage)" },
  { pid: "AA1419002-E6",   formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 10000, reachLabel: "LX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "1000BASE-LX", notes: "Avaya 1G SFP LX 10km SM" },
  { pid: "AA1419003-E6",   formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 40000, reachLabel: "EX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "1000BASE-EX", notes: "Avaya 1G SFP EX 40km SM" },
  { pid: "AA1419004-E6",   formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 80000, reachLabel: "ZX",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "1000BASE-ZX", notes: "Avaya 1G SFP ZX 80km SM" },
  { pid: "AA1419005-E6",   formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 100,   reachLabel: "T",    fiberType: "CU",  connector: "RJ-45",                       standard: "1000BASE-T",  notes: "Avaya 1G copper SFP RJ45" },
  { pid: "AA1419039-E6",   formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 10000, reachLabel: "BX-U", fiberType: "SMF", connector: "LC",  wavelengths: "1310nm TX / 1490nm RX", notes: "Avaya 1G BiDi SFP BX-U" },
  { pid: "AA1419040-E6",   formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 10000, reachLabel: "BX-D", fiberType: "SMF", connector: "LC",  wavelengths: "1490nm TX / 1310nm RX", notes: "Avaya 1G BiDi SFP BX-D" },
  // ── 10G SFP+ — ERS 5900/VSP 4850/7200 ────────────────────────────────
  { pid: "AA1419049-E6",   formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 300,   reachLabel: "SR",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  standard: "10GBASE-SR",  notes: "Avaya 10G SFP+ SR 300m — ERS 5900/VSP 4850" },
  { pid: "AA1419050-E6",   formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 10000, reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "10GBASE-LR",  notes: "Avaya 10G SFP+ LR 10km SM" },
  { pid: "AA1419051-E6",   formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 40000, reachLabel: "ER",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "10GBASE-ER",  notes: "Avaya 10G SFP+ ER 40km SM" },
  { pid: "AA1419052-E6",   formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 80000, reachLabel: "ZR",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "10GBASE-ZR",  notes: "Avaya 10G SFP+ ZR 80km SM" },
  // ── Newer 700480xxx numbering — VSP 7200/8200/9000 ────────────────────
  { pid: "700480566",      formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 300,   reachLabel: "SR",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  standard: "10GBASE-SR",  notes: "Avaya VSP 7200 10G SFP+ SR 300m" },
  { pid: "700480567",      formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 10000, reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "10GBASE-LR",  notes: "Avaya VSP 7200 10G SFP+ LR 10km" },
  { pid: "700480568",      formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 40000, reachLabel: "ER",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "10GBASE-ER",  notes: "Avaya VSP 7200 10G SFP+ ER 40km" },
  // ── QSFP+ 40G — VSP 8200/9000 ────────────────────────────────────────
  { pid: "700480575",      formFactor: "QSFP+", speedGbps: 40,  speed: "40G",  reachMeters: 150,   reachLabel: "SR4",  fiberType: "MMF", connector: "MPO", wavelengths: "850nm",  standard: "40GBASE-SR4", notes: "Avaya VSP 8200 40G QSFP+ SR4 MPO" },
  { pid: "700480576",      formFactor: "QSFP+", speedGbps: 40,  speed: "40G",  reachMeters: 10000, reachLabel: "LR4",  fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "40GBASE-LR4", notes: "Avaya VSP 8200 40G QSFP+ LR4 LC" },
  // ── QSFP28 100G — VSP 9000 high-core ────────────────────────────────
  { pid: "700480580",      formFactor: "QSFP28",speedGbps: 100, speed: "100G", reachMeters: 100,   reachLabel: "SR4",  fiberType: "MMF", connector: "MPO", wavelengths: "850nm",  standard: "100GBASE-SR4",notes: "Avaya VSP 9000 100G QSFP28 SR4 MPO" },
  { pid: "700480581",      formFactor: "QSFP28",speedGbps: 100, speed: "100G", reachMeters: 10000, reachLabel: "LR4",  fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "100GBASE-LR4",notes: "Avaya VSP 9000 100G QSFP28 LR4 LC" },
];

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

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

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

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

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