/**
 * iSolan OEM Transceiver Catalog Seed
 *
 * Seeds iSolan-branded transceiver PIDs for optical components and media
 * converters. iSolan is a German manufacturer specializing in fiber optic
 * transceivers, CWDM/BiDi SFPs, and media conversion for enterprise and
 * carrier access networks.
 *
 * Sources:
 *  - iSolan Product Catalog (isolan.com/products/sfp)
 *  - iSolan SFP/SFP+ Datasheet Series (ISL-SFP-*)
 *  - iSolan CWDM SFP Specification Sheet (8-channel 1470-1610nm)
 *  - iSolan QSFP+ / SFP28 Product Range
 *
 * Run:  tsx packages/scraper/src/scrapers/isolan-oem.ts
 * Cron: daily at 00:45
 */

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

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

const ISOLAN_PIDS: IsolanPID[] = [
  // ── 1G SFP ──────────────────────────────────────────────────────────────
  { pid: "ISL-SFP-1G-SX",       formFactor: "SFP",   speedGbps: 1,   speed: "1G",  reachMeters: 550,     reachLabel: "SX",         fiberType: "MMF", connector: "LC",  wavelengths: "850nm",   standard: "1000BASE-SX" },
  { pid: "ISL-SFP-1G-LX",       formFactor: "SFP",   speedGbps: 1,   speed: "1G",  reachMeters: 10000,   reachLabel: "LX",         fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",  standard: "1000BASE-LX" },
  { pid: "ISL-SFP-1G-ZX",       formFactor: "SFP",   speedGbps: 1,   speed: "1G",  reachMeters: 80000,   reachLabel: "ZX",         fiberType: "SMF", connector: "LC",  wavelengths: "1550nm",  standard: "1000BASE-ZX", notes: "iSolan 80km SMF extended reach SFP" },

  // ── 10G SFP+ ────────────────────────────────────────────────────────────
  { pid: "ISL-SFP-10G-SR",      formFactor: "SFP+",  speedGbps: 10,  speed: "10G", reachMeters: 300,     reachLabel: "SR",         fiberType: "MMF", connector: "LC",  wavelengths: "850nm",   standard: "10GBASE-SR" },
  { pid: "ISL-SFP-10G-LR",      formFactor: "SFP+",  speedGbps: 10,  speed: "10G", reachMeters: 10000,   reachLabel: "LR",         fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",  standard: "10GBASE-LR" },
  { pid: "ISL-SFP-10G-ER",      formFactor: "SFP+",  speedGbps: 10,  speed: "10G", reachMeters: 40000,   reachLabel: "ER",         fiberType: "SMF", connector: "LC",  wavelengths: "1550nm",  standard: "10GBASE-ER" },
  { pid: "ISL-SFP-10G-ZR",      formFactor: "SFP+",  speedGbps: 10,  speed: "10G", reachMeters: 80000,   reachLabel: "ZR",         fiberType: "SMF", connector: "LC",  wavelengths: "1550nm",  standard: "10GBASE-ZR", category: "Telecom", notes: "iSolan 80km ZR SFP+ for long-haul access links" },

  // ── CWDM SFP (8-channel 1470–1610nm) ────────────────────────────────────
  { pid: "ISL-SFP-CWDM-1470",   formFactor: "SFP",   speedGbps: 1,   speed: "1G",  reachMeters: 40000,   reachLabel: "CWDM-1470",  fiberType: "SMF", connector: "LC",  wavelengths: "1470nm",  standard: "1000BASE-CWDM", category: "Telecom", notes: "iSolan CWDM SFP ch1 1470nm, 40km SMF" },
  { pid: "ISL-SFP-CWDM-1490",   formFactor: "SFP",   speedGbps: 1,   speed: "1G",  reachMeters: 40000,   reachLabel: "CWDM-1490",  fiberType: "SMF", connector: "LC",  wavelengths: "1490nm",  standard: "1000BASE-CWDM", category: "Telecom", notes: "iSolan CWDM SFP ch2 1490nm, 40km SMF" },
  { pid: "ISL-SFP-CWDM-1510",   formFactor: "SFP",   speedGbps: 1,   speed: "1G",  reachMeters: 40000,   reachLabel: "CWDM-1510",  fiberType: "SMF", connector: "LC",  wavelengths: "1510nm",  standard: "1000BASE-CWDM", category: "Telecom", notes: "iSolan CWDM SFP ch3 1510nm, 40km SMF" },
  { pid: "ISL-SFP-CWDM-1530",   formFactor: "SFP",   speedGbps: 1,   speed: "1G",  reachMeters: 40000,   reachLabel: "CWDM-1530",  fiberType: "SMF", connector: "LC",  wavelengths: "1530nm",  standard: "1000BASE-CWDM", category: "Telecom", notes: "iSolan CWDM SFP ch4 1530nm, 40km SMF" },
  { pid: "ISL-SFP-CWDM-1550",   formFactor: "SFP",   speedGbps: 1,   speed: "1G",  reachMeters: 40000,   reachLabel: "CWDM-1550",  fiberType: "SMF", connector: "LC",  wavelengths: "1550nm",  standard: "1000BASE-CWDM", category: "Telecom", notes: "iSolan CWDM SFP ch5 1550nm, 40km SMF" },
  { pid: "ISL-SFP-CWDM-1570",   formFactor: "SFP",   speedGbps: 1,   speed: "1G",  reachMeters: 40000,   reachLabel: "CWDM-1570",  fiberType: "SMF", connector: "LC",  wavelengths: "1570nm",  standard: "1000BASE-CWDM", category: "Telecom", notes: "iSolan CWDM SFP ch6 1570nm, 40km SMF" },
  { pid: "ISL-SFP-CWDM-1590",   formFactor: "SFP",   speedGbps: 1,   speed: "1G",  reachMeters: 40000,   reachLabel: "CWDM-1590",  fiberType: "SMF", connector: "LC",  wavelengths: "1590nm",  standard: "1000BASE-CWDM", category: "Telecom", notes: "iSolan CWDM SFP ch7 1590nm, 40km SMF" },
  { pid: "ISL-SFP-CWDM-1610",   formFactor: "SFP",   speedGbps: 1,   speed: "1G",  reachMeters: 40000,   reachLabel: "CWDM-1610",  fiberType: "SMF", connector: "LC",  wavelengths: "1610nm",  standard: "1000BASE-CWDM", category: "Telecom", notes: "iSolan CWDM SFP ch8 1610nm, 40km SMF" },

  // ── BiDi SFP ─────────────────────────────────────────────────────────────
  { pid: "ISL-SFP-BIDI-1310",   formFactor: "SFP",   speedGbps: 1,   speed: "1G",  reachMeters: 20000,   reachLabel: "BiDi-1310",  fiberType: "SMF", connector: "LC",  wavelengths: "1310nm TX / 1490nm RX", standard: "1000BASE-BX", category: "Telecom", notes: "iSolan BiDi SFP 1310TX/1490RX single-fiber WDM" },

  // ── 40G QSFP+ ───────────────────────────────────────────────────────────
  { pid: "ISL-QSFP-40G-SR4",    formFactor: "QSFP+",  speedGbps: 40,  speed: "40G", reachMeters: 150,     reachLabel: "SR4",        fiberType: "MMF", connector: "MPO", wavelengths: "850nm",   standard: "40GBASE-SR4" },
  { pid: "ISL-QSFP-40G-LR4",    formFactor: "QSFP+",  speedGbps: 40,  speed: "40G", reachMeters: 10000,   reachLabel: "LR4",        fiberType: "SMF", connector: "LC",  wavelengths: "1270-1330nm", standard: "40GBASE-LR4" },

  // ── 25G SFP28 ───────────────────────────────────────────────────────────
  { pid: "ISL-SFP28-25G-SR",    formFactor: "SFP28",  speedGbps: 25,  speed: "25G", reachMeters: 100,     reachLabel: "SR",         fiberType: "MMF", connector: "LC",  wavelengths: "850nm",   standard: "25GBASE-SR" },
  { pid: "ISL-SFP28-25G-LR",    formFactor: "SFP28",  speedGbps: 25,  speed: "25G", reachMeters: 10000,   reachLabel: "LR",         fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",  standard: "25GBASE-LR" },
];

// PIDs that belong to the Telecom category (CWDM, BiDi, ZR)
const TELECOM_PIDS = new Set([
  "ISL-SFP-10G-ZR",
  "ISL-SFP-CWDM-1470",
  "ISL-SFP-CWDM-1490",
  "ISL-SFP-CWDM-1510",
  "ISL-SFP-CWDM-1530",
  "ISL-SFP-CWDM-1550",
  "ISL-SFP-CWDM-1570",
  "ISL-SFP-CWDM-1590",
  "ISL-SFP-CWDM-1610",
  "ISL-SFP-BIDI-1310",
]);

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

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

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

  for (const p of ISOLAN_PIDS) {
    const slug     = `isolan-${p.pid.toLowerCase().replace(/[^a-z0-9]+/g, "-")}`;
    const category = p.category ?? (TELECOM_PIDS.has(p.pid) ? "Telecom" : "DataCenter");
    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',$13,$14)
         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, category, 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=== iSolan OEM Seed Complete ===`);
  console.log(`  Inserted: ${inserted}`);
  console.log(`  Updated:  ${updated}`);
  console.log(`  Errors:   ${errors}`);
  console.log(`  Total PIDs: ${ISOLAN_PIDS.length}\n`);
}

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