/**
 * HPE Storage OEM Transceiver Catalog Seed
 *
 * Seeds HPE Storage-branded transceiver PIDs for 3PAR/Primera/Alletra SAN
 * storage controllers, FC host bus adapters, and iSCSI/FCoE host ports.
 * Covers the full HPE StoreServ / Nimble / Alletra optical module lineup.
 *
 * Sources:
 *  - HPE Storage Compatibility Matrix (support.hpe.com)
 *  - HPE 3PAR/Primera/Alletra Hardware Installation Guides
 *  - HPE QuickSpecs for SAN Switch and Storage optical modules
 *
 * Run:  tsx packages/scraper/src/scrapers/hpe-storage-oem.ts
 * Cron: daily at 06:15
 */

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

interface HpeStoragePID {
  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  (8GFC)
//   16G = 14.025 Gbps  (16GFC)
//   32G = 28.05  Gbps  (32GFC)
const HPE_STORAGE_PIDS: HpeStoragePID[] = [
  // ── 8G FC SFP+ ───────────────────────────────────────────────────────────
  {
    pid:         "AJ716B",
    formFactor:  "SFP+",
    speedGbps:   8.5,
    speed:       "8G FC",
    reachMeters: 150,
    reachLabel:  "SW",
    fiberType:   "MMF",
    connector:   "LC",
    wavelengths: "850nm",
    standard:    "8GFC",
    notes:       "8G FC SFP+ SW; HPE 3PAR/P9000 FC host/target port; OM3/OM4 MMF up to 150m",
  },
  {
    pid:         "AJ717B",
    formFactor:  "SFP+",
    speedGbps:   8.5,
    speed:       "8G FC",
    reachMeters: 10000,
    reachLabel:  "LW",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1310nm",
    standard:    "8GFC",
    notes:       "8G FC SFP+ LW; HPE 3PAR/P9000 FC host/target port long-reach; OS2 SMF up to 10km",
  },
  // ── 16G FC SFP+ ──────────────────────────────────────────────────────────
  {
    pid:         "C8R23B",
    formFactor:  "SFP+",
    speedGbps:   14.025,
    speed:       "16G FC",
    reachMeters: 125,
    reachLabel:  "SW",
    fiberType:   "MMF",
    connector:   "LC",
    wavelengths: "850nm",
    standard:    "16GFC",
    notes:       "16G FC SFP+ SW; HPE 3PAR 7000/8000/Primera FC target port; OM4 MMF up to 125m",
  },
  {
    pid:         "C8R24B",
    formFactor:  "SFP+",
    speedGbps:   14.025,
    speed:       "16G FC",
    reachMeters: 10000,
    reachLabel:  "LW",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1310nm",
    standard:    "16GFC",
    notes:       "16G FC SFP+ LW; HPE 3PAR 7000/8000/Primera FC target port long-reach; OS2 SMF up to 10km",
  },
  // ── 32G FC SFP28 ─────────────────────────────────────────────────────────
  {
    pid:         "Q2P63A",
    formFactor:  "SFP28",
    speedGbps:   28.05,
    speed:       "32G FC",
    reachMeters: 100,
    reachLabel:  "SW",
    fiberType:   "MMF",
    connector:   "LC",
    wavelengths: "850nm",
    standard:    "32GFC",
    notes:       "32G FC SFP28 SW; HPE Primera A630/A650/Alletra 9000 FC NVMe/FC port; OM4/OM5 MMF",
  },
  {
    pid:         "Q2P64A",
    formFactor:  "SFP28",
    speedGbps:   28.05,
    speed:       "32G FC",
    reachMeters: 10000,
    reachLabel:  "LW",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1310nm",
    standard:    "32GFC",
    notes:       "32G FC SFP28 LW; HPE Primera A630/A650/Alletra 9000 FC NVMe/FC long-reach; OS2 SMF",
  },
  // ── 10GbE SFP+ ───────────────────────────────────────────────────────────
  {
    pid:         "455885-B21",
    formFactor:  "SFP+",
    speedGbps:   10,
    speed:       "10G",
    reachMeters: 300,
    reachLabel:  "SR",
    fiberType:   "MMF",
    connector:   "LC",
    wavelengths: "850nm",
    standard:    "10GBASE-SR",
    notes:       "10GbE SFP+ SR; HPE storage and server host port; OM3 300m / OM4 400m MMF",
  },
  {
    pid:         "455886-B21",
    formFactor:  "SFP+",
    speedGbps:   10,
    speed:       "10G",
    reachMeters: 10000,
    reachLabel:  "LR",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1310nm",
    standard:    "10GBASE-LR",
    notes:       "10GbE SFP+ LR; HPE storage and server host port long-reach; OS2 SMF up to 10km",
  },
  // ── 25GbE SFP28 ──────────────────────────────────────────────────────────
  {
    pid:         "813874-B21",
    formFactor:  "SFP28",
    speedGbps:   25,
    speed:       "25G",
    reachMeters: 100,
    reachLabel:  "SR",
    fiberType:   "MMF",
    connector:   "LC",
    wavelengths: "850nm",
    standard:    "25GBASE-SR",
    notes:       "25GbE SFP28 SR; HPE Alletra 6000/9000 iSCSI/NVMe-oF host port; OM4 MMF",
  },
  {
    pid:         "813875-B21",
    formFactor:  "SFP28",
    speedGbps:   25,
    speed:       "25G",
    reachMeters: 10000,
    reachLabel:  "LR",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1310nm",
    standard:    "25GBASE-LR",
    notes:       "25GbE SFP28 LR; HPE Alletra 6000/9000 iSCSI/NVMe-oF host port long-reach; OS2 SMF",
  },
  // ── 100GbE QSFP28 ────────────────────────────────────────────────────────
  {
    pid:         "817753-B21",
    formFactor:  "QSFP28",
    speedGbps:   100,
    speed:       "100G",
    reachMeters: 100,
    reachLabel:  "SR4",
    fiberType:   "MMF",
    connector:   "MPO",
    wavelengths: "850nm",
    standard:    "100GBASE-SR4",
    notes:       "100GbE QSFP28 SR4; HPE storage backend/replication interconnect; OM4 MPO-12",
  },
  {
    pid:         "817754-B21",
    formFactor:  "QSFP28",
    speedGbps:   100,
    speed:       "100G",
    reachMeters: 10000,
    reachLabel:  "LR4",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1295-1310nm",
    standard:    "100GBASE-LR4",
    notes:       "100GbE QSFP28 LR4; HPE storage backend/replication long-reach; OS2 SMF up to 10km",
  },
  // ── 400GbE QSFP-DD ───────────────────────────────────────────────────────
  {
    pid:         "P08748-B21",
    formFactor:  "QSFP-DD",
    speedGbps:   400,
    speed:       "400G",
    reachMeters: 500,
    reachLabel:  "DR4",
    fiberType:   "SMF",
    connector:   "MPO",
    wavelengths: "1310nm",
    standard:    "400GBASE-DR4",
    notes:       "400GbE QSFP-DD DR4; HPE next-gen storage fabric interconnect; OS2 SMF up to 500m",
  },
  // ── 1GbE SFP ─────────────────────────────────────────────────────────────
  {
    pid:         "453154-B21",
    formFactor:  "SFP",
    speedGbps:   1,
    speed:       "1G",
    reachMeters: 550,
    reachLabel:  "SX",
    fiberType:   "MMF",
    connector:   "LC",
    wavelengths: "850nm",
    standard:    "1000BASE-SX",
    notes:       "1GbE SFP SX; HPE storage management and iSCSI port; OM2/OM3 MMF",
  },
  {
    pid:         "453156-B21",
    formFactor:  "SFP",
    speedGbps:   1,
    speed:       "1G",
    reachMeters: 10000,
    reachLabel:  "LX",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1310nm",
    standard:    "1000BASE-LX",
    notes:       "1GbE SFP LX; HPE storage management and iSCSI port long-reach; OS2 SMF",
  },
  // ── Copper SFP ───────────────────────────────────────────────────────────
  {
    pid:         "HPE-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; HPE storage management port copper option; Cat5e/6",
  },
  {
    pid:         "HPE-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; HPE iSCSI storage host port; Cat6a up to 30m",
  },
  // ── 40GbE QSFP+ ──────────────────────────────────────────────────────────
  {
    pid:         "HPE-QSFP-40G-SR4",
    formFactor:  "QSFP+",
    speedGbps:   40,
    speed:       "40G",
    reachMeters: 150,
    reachLabel:  "SR4",
    fiberType:   "MMF",
    connector:   "MPO",
    wavelengths: "850nm",
    standard:    "40GBASE-SR4",
    notes:       "40GbE QSFP+ SR4; HPE storage fabric switch uplink; OM3/OM4 MPO-12",
  },
  {
    pid:         "HPE-QSFP-40G-LR4",
    formFactor:  "QSFP+",
    speedGbps:   40,
    speed:       "40G",
    reachMeters: 10000,
    reachLabel:  "LR4",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1310nm",
    standard:    "40GBASE-LR4",
    notes:       "40GbE QSFP+ LR4; HPE storage fabric switch uplink long-reach; OS2 SMF",
  },
  // ── 16G FC SFP+ Long-Wave (generic HPE part) ─────────────────────────────
  {
    pid:         "HPE-SFP-16G-FC-LW",
    formFactor:  "SFP+",
    speedGbps:   14.025,
    speed:       "16G FC",
    reachMeters: 10000,
    reachLabel:  "LW",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1310nm",
    standard:    "16GFC",
    notes:       "16G FC SFP+ LW generic HPE part; 3PAR/Primera cross-compat long-wave; OS2 SMF",
  },
];

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

  const vendorId = await ensureVendor(
    "HPE Storage",
    "oem",
    "https://www.hpe.com/storage",
    undefined
  );

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

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

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