/**
 * NetApp OEM Transceiver Catalog Seed
 *
 * Seeds NetApp-branded transceiver PIDs for ONTAP cluster interconnect,
 * FAS/AFF storage controllers, and SAN host ports. NetApp uses an X-prefix
 * naming scheme (X6xxx / X66xxx series) for optical modules.
 *
 * Sources:
 *  - NetApp Hardware Universe (hwu.netapp.com)
 *  - NetApp ONTAP Storage Hardware Guide
 *  - NetApp FAS/AFF system-level optical compatibility matrices
 *
 * Run:  tsx packages/scraper/src/scrapers/netapp-oem.ts
 * Cron: daily at 05:45
 */

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

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

// Fibre Channel line-rate speeds (Gbps):
//   8G  = 8.5    Gbps  (4x FC)
//   16G = 14.025 Gbps  (8x FC)
//   32G = 28.05  Gbps  (16x FC)
const NETAPP_PIDS: NetappPID[] = [
  // ── 1G SFP ───────────────────────────────────────────────────────────────
  {
    pid:         "X6565-R6",
    formFactor:  "SFP",
    speedGbps:   1,
    speed:       "1G",
    reachMeters: 550,
    reachLabel:  "SX",
    fiberType:   "MMF",
    connector:   "LC",
    wavelengths: "850nm",
    standard:    "1000BASE-SX",
    notes:       "1GbE SFP SX; NetApp FAS/AFF management and data ports; OM2/OM3 MMF",
  },
  {
    pid:         "X6566-R6",
    formFactor:  "SFP",
    speedGbps:   1,
    speed:       "1G",
    reachMeters: 10000,
    reachLabel:  "LX",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1310nm",
    standard:    "1000BASE-LX",
    notes:       "1GbE SFP LX; NetApp FAS/AFF management and data ports; OS2 SMF",
  },
  // ── 10G SFP+ ─────────────────────────────────────────────────────────────
  {
    pid:         "X6567-R6",
    formFactor:  "SFP+",
    speedGbps:   10,
    speed:       "10G",
    reachMeters: 300,
    reachLabel:  "SR",
    fiberType:   "MMF",
    connector:   "LC",
    wavelengths: "850nm",
    standard:    "10GBASE-SR",
    notes:       "10GbE SFP+ SR; NetApp cluster interconnect and host ports; OM3/OM4 MMF",
  },
  {
    pid:         "X6568-R6",
    formFactor:  "SFP+",
    speedGbps:   10,
    speed:       "10G",
    reachMeters: 10000,
    reachLabel:  "LR",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1310nm",
    standard:    "10GBASE-LR",
    notes:       "10GbE SFP+ LR; NetApp cluster interconnect and host ports; OS2 SMF",
  },
  {
    pid:         "X6569-R6",
    formFactor:  "SFP+",
    speedGbps:   10,
    speed:       "10G",
    reachMeters: 40000,
    reachLabel:  "ER",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1550nm",
    standard:    "10GBASE-ER",
    notes:       "10GbE SFP+ ER; extended-reach NetApp data-fabric links; OS2 SMF up to 40km",
  },
  // ── 8G FC SFP ────────────────────────────────────────────────────────────
  {
    pid:         "X6596-R6",
    formFactor:  "SFP",
    speedGbps:   8.5,
    speed:       "8G FC",
    reachMeters: 150,
    reachLabel:  "SW",
    fiberType:   "MMF",
    connector:   "LC",
    wavelengths: "850nm",
    standard:    "8GFC",
    notes:       "8G FC short-wave; NetApp FAS/AFF FC target port; OM3/OM4 MMF",
  },
  // ── 16G FC SFP ───────────────────────────────────────────────────────────
  {
    pid:         "X6597-R6",
    formFactor:  "SFP",
    speedGbps:   14.025,
    speed:       "16G FC",
    reachMeters: 125,
    reachLabel:  "SW",
    fiberType:   "MMF",
    connector:   "LC",
    wavelengths: "850nm",
    standard:    "16GFC",
    notes:       "16G FC short-wave; NetApp FAS/AFF FC target port; OM4 MMF",
  },
  {
    pid:         "X6598-R6",
    formFactor:  "SFP",
    speedGbps:   14.025,
    speed:       "16G FC",
    reachMeters: 10000,
    reachLabel:  "LW",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1310nm",
    standard:    "16GFC",
    notes:       "16G FC long-wave; NetApp FAS/AFF FC target port; OS2 SMF up to 10km",
  },
  // ── 32G FC SFP+ ──────────────────────────────────────────────────────────
  {
    pid:         "X6599-R6",
    formFactor:  "SFP+",
    speedGbps:   28.05,
    speed:       "32G FC",
    reachMeters: 100,
    reachLabel:  "SW",
    fiberType:   "MMF",
    connector:   "LC",
    wavelengths: "850nm",
    standard:    "32GFC",
    notes:       "32G FC short-wave; NetApp AFF FC NVMe/FC host port; OM4/OM5 MMF",
  },
  // ── 25G SFP28 ────────────────────────────────────────────────────────────
  {
    pid:         "X66030A",
    formFactor:  "SFP28",
    speedGbps:   25,
    speed:       "25G",
    reachMeters: 100,
    reachLabel:  "SR",
    fiberType:   "MMF",
    connector:   "LC",
    wavelengths: "850nm",
    standard:    "25GBASE-SR",
    notes:       "25GbE SFP28 SR; NetApp AFF A-series cluster interconnect; OM4 MMF",
  },
  {
    pid:         "X66031A",
    formFactor:  "SFP28",
    speedGbps:   25,
    speed:       "25G",
    reachMeters: 10000,
    reachLabel:  "LR",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1310nm",
    standard:    "25GBASE-LR",
    notes:       "25GbE SFP28 LR; NetApp AFF A-series cluster interconnect; OS2 SMF",
  },
  // ── 40G QSFP+ ────────────────────────────────────────────────────────────
  {
    pid:         "X66240A",
    formFactor:  "QSFP+",
    speedGbps:   40,
    speed:       "40G",
    reachMeters: 150,
    reachLabel:  "SR4",
    fiberType:   "MMF",
    connector:   "MPO",
    wavelengths: "850nm",
    standard:    "40GBASE-SR4",
    notes:       "40GbE QSFP+ SR4; NetApp cluster interconnect switch uplinks; OM3/OM4 MMF",
  },
  {
    pid:         "X66100A",
    formFactor:  "QSFP+",
    speedGbps:   40,
    speed:       "40G",
    reachMeters: 10000,
    reachLabel:  "LR4",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1310nm",
    standard:    "40GBASE-LR4",
    notes:       "40GbE QSFP+ LR4; NetApp cluster interconnect long-reach; OS2 SMF",
  },
  // ── 100G QSFP28 ──────────────────────────────────────────────────────────
  {
    pid:         "X66250A",
    formFactor:  "QSFP28",
    speedGbps:   100,
    speed:       "100G",
    reachMeters: 100,
    reachLabel:  "SR4",
    fiberType:   "MMF",
    connector:   "MPO",
    wavelengths: "850nm",
    standard:    "100GBASE-SR4",
    notes:       "100GbE QSFP28 SR4; NetApp AFF A800/C800 cluster interconnect; OM4 MMF",
  },
  {
    pid:         "X66260A",
    formFactor:  "QSFP28",
    speedGbps:   100,
    speed:       "100G",
    reachMeters: 10000,
    reachLabel:  "LR4",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1295-1310nm",
    standard:    "100GBASE-LR4",
    notes:       "100GbE QSFP28 LR4; NetApp AFF A800/C800 cluster interconnect; OS2 SMF",
  },
  // ── Copper SFP ───────────────────────────────────────────────────────────
  {
    pid:         "NETAPP-SFP-1G-T",
    formFactor:  "SFP",
    speedGbps:   1,
    speed:       "1G",
    reachMeters: 100,
    reachLabel:  "T",
    fiberType:   "DAC",
    connector:   "RJ45",
    standard:    "1000BASE-T",
    notes:       "1GbE copper SFP RJ45; NetApp management port copper option; Cat5e/6",
  },
  {
    pid:         "NETAPP-SFP-10G-T",
    formFactor:  "SFP+",
    speedGbps:   10,
    speed:       "10G",
    reachMeters: 30,
    reachLabel:  "T",
    fiberType:   "DAC",
    connector:   "RJ45",
    standard:    "10GBASE-T",
    notes:       "10GbE copper SFP+ RJ45; NetApp data port copper option; Cat6a",
  },
  // ── AOC / DAC ────────────────────────────────────────────────────────────
  {
    pid:         "X66210A",
    formFactor:  "QSFP28",
    speedGbps:   100,
    speed:       "100G",
    reachMeters: 30,
    reachLabel:  "AOC",
    fiberType:   "AOC",
    connector:   "QSFP28",
    notes:       "100G Active Optical Cable (AOC); NetApp cluster interconnect short-reach; 30m",
  },
  {
    pid:         "X66220A",
    formFactor:  "SFP28",
    speedGbps:   25,
    speed:       "25G",
    reachMeters: 1,
    reachLabel:  "DAC-1m",
    fiberType:   "DAC",
    connector:   "SFP28",
    notes:       "25G Direct Attach Copper 1m; NetApp cluster interconnect; passive twinax",
  },
  {
    pid:         "X66230A",
    formFactor:  "QSFP28",
    speedGbps:   100,
    speed:       "100G",
    reachMeters: 3,
    reachLabel:  "DAC-3m",
    fiberType:   "DAC",
    connector:   "QSFP28",
    notes:       "100G Direct Attach Copper 3m; NetApp cluster interconnect; passive twinax",
  },
];

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

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

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

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

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