/**
 * 3Com (Legacy) OEM Transceiver Catalog Seed
 *
 * 3Com Corporation (acquired by HP in 2010, now HPE) made Switch 5500,
 * Switch 7750, and H3C-co-developed enterprise switches. Their SFP PIDs
 * (3C17770/3C17771 family and H3C-branded) appear in campus network
 * upgrade projects and legacy compatibility matrices.
 *
 * Sources:
 *  - 3Com Switch 5500/7750 product documentation
 *  - 3Com SFP transceiver compatibility guide
 *  - H3C (3Com/HP joint venture) optics catalog
 *
 * Run:  tsx packages/scraper/src/scrapers/3com-legacy-oem.ts
 * Cron: daily at 06:00
 */

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

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

const THREECOM_PIDS: ThreeComPID[] = [
  // ── 1G SFP — Switch 5500/7750 series ─────────────────────────────────
  { pid: "3CSFP91",    formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 550,   reachLabel: "SX",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  standard: "1000BASE-SX", notes: "3Com 1G SFP SX 550m MM — Switch 5500/7750" },
  { pid: "3CSFP92",    formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 10000, reachLabel: "LX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "1000BASE-LX", notes: "3Com 1G SFP LX 10km SM" },
  { pid: "3CSFP93",    formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 40000, reachLabel: "EX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "1000BASE-EX", notes: "3Com 1G SFP EX 40km SM" },
  { pid: "3CSFP94",    formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 80000, reachLabel: "ZX",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "1000BASE-ZX", notes: "3Com 1G SFP ZX 80km SM" },
  { pid: "3CSFP95",    formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 100,   reachLabel: "T",    fiberType: "CU",  connector: "RJ-45",                       standard: "1000BASE-T",  notes: "3Com 1G copper SFP RJ45" },
  { pid: "3C17770",    formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 550,   reachLabel: "SX",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  standard: "1000BASE-SX", notes: "3Com Switch 5500 1G SFP SX module" },
  { pid: "3C17771",    formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 10000, reachLabel: "LX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "1000BASE-LX", notes: "3Com Switch 5500 1G SFP LX module" },
  { pid: "3C17772",    formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 70000, reachLabel: "ZX",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "1000BASE-ZX", notes: "3Com Switch 7750 1G SFP ZX module" },
  // ── 1G SFP BiDi ──────────────────────────────────────────────────────
  { pid: "3C17789",    formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 20000, reachLabel: "BX-U", fiberType: "SMF", connector: "LC",  wavelengths: "1310nm TX / 1490nm RX", notes: "3Com 1G BiDi SFP BX-U 20km" },
  { pid: "3C17790",    formFactor: "SFP",   speedGbps: 1,  speed: "1G",  reachMeters: 20000, reachLabel: "BX-D", fiberType: "SMF", connector: "LC",  wavelengths: "1490nm TX / 1310nm RX", notes: "3Com 1G BiDi SFP BX-D 20km" },
  // ── 10G SFP+ — Switch 7750 / SuperStack 3 ────────────────────────────
  { pid: "3C17780",    formFactor: "SFP+",  speedGbps: 10, speed: "10G", reachMeters: 300,   reachLabel: "SR",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  standard: "10GBASE-SR",  notes: "3Com 10G SFP+ SR 300m MM" },
  { pid: "3C17781",    formFactor: "SFP+",  speedGbps: 10, speed: "10G", reachMeters: 10000, reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "10GBASE-LR",  notes: "3Com 10G SFP+ LR 10km SM" },
  { pid: "3C17782",    formFactor: "SFP+",  speedGbps: 10, speed: "10G", reachMeters: 40000, reachLabel: "ER",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "10GBASE-ER",  notes: "3Com 10G SFP+ ER 40km SM" },
  { pid: "3C17783",    formFactor: "SFP+",  speedGbps: 10, speed: "10G", reachMeters: 80000, reachLabel: "ZR",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "10GBASE-ZR",  notes: "3Com 10G SFP+ ZR 80km SM" },
  // ── XFP — 3Com Router 6040 ───────────────────────────────────────────
  { pid: "3CXFP91",    formFactor: "XFP",   speedGbps: 10, speed: "10G", reachMeters: 300,   reachLabel: "SR",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  standard: "10GBASE-SR",  notes: "3Com 10G XFP SR 300m — Router 6040" },
  { pid: "3CXFP92",    formFactor: "XFP",   speedGbps: 10, speed: "10G", reachMeters: 10000, reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "10GBASE-LR",  notes: "3Com 10G XFP LR 10km SM" },
  // ── GBIC — legacy Switch 4500/4400 ───────────────────────────────────
  { pid: "3C16698",    formFactor: "GBIC",  speedGbps: 1,  speed: "1G",  reachMeters: 550,   reachLabel: "SX",   fiberType: "MMF", connector: "SC",  wavelengths: "850nm",  standard: "1000BASE-SX", notes: "3Com 1G GBIC SX 550m SC — Switch 4500" },
  { pid: "3C16699",    formFactor: "GBIC",  speedGbps: 1,  speed: "1G",  reachMeters: 10000, reachLabel: "LX",   fiberType: "SMF", connector: "SC",  wavelengths: "1310nm", standard: "1000BASE-LX", notes: "3Com 1G GBIC LX 10km SC SM" },
];

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

  const vendorId = await ensureVendor(
    "3Com",
    "oem",
    "https://www.hpe.com",
    undefined
  );

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

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

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