/**
 * EnGenius OEM Transceiver Catalog Seed
 *
 * EnGenius (brand of Senao International) focuses on enterprise switching,
 * wireless APs, and outdoor CPE for SMB/WISP deployments. Their ECS managed
 * switch family (ECS1552FP, ECS4100, ECS5512-54P) supports SFP/SFP+/QSFP28.
 * EnGenius-branded SFPs are sold separately from Senao OEM modules.
 *
 * Sources:
 *  - EnGenius ECS switch SFP compatibility matrix
 *  - EnGenius Smart/Cloud switch accessory guide 2023
 *  - EnGenius SFP/SFP+ module datasheet
 *
 * Run:  tsx packages/scraper/src/scrapers/engenius-oem.ts
 * Cron: daily at 04:30
 */

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

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

const ENGENIUS_PIDS: EngeniusPID[] = [
  // ── SFP 1G — ECS1552FP/ECS4100 GigE ports ────────────────────────────
  { pid: "ECS-SFP-1G-SX",    formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 550,   reachLabel: "SX",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  standard: "1000BASE-SX", notes: "EnGenius 1G SFP 850nm 550m MM for ECS switches" },
  { pid: "ECS-SFP-1G-LX",    formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 10000, reachLabel: "LX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "1000BASE-LX", notes: "EnGenius 1G SFP 1310nm 10km SM" },
  { pid: "ECS-SFP-1G-ZX",    formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 80000, reachLabel: "ZX",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "1000BASE-ZX", notes: "EnGenius 1G SFP 1550nm 80km SM" },
  { pid: "ECS-SFP-1G-T",     formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 100,   reachLabel: "T",    fiberType: "CU",  connector: "RJ-45",                       standard: "1000BASE-T",  notes: "EnGenius 1G copper SFP RJ45" },
  { pid: "ECS-SFP-1G-BXU20", formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 20000, reachLabel: "BX-U", fiberType: "SMF", connector: "LC",  wavelengths: "1310nm TX / 1550nm RX", notes: "EnGenius 1G BiDi SFP Tx1310 upstream 20km" },
  { pid: "ECS-SFP-1G-BXD20", formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 20000, reachLabel: "BX-D", fiberType: "SMF", connector: "LC",  wavelengths: "1550nm TX / 1310nm RX", notes: "EnGenius 1G BiDi SFP Tx1550 downstream 20km" },
  // ── SFP+ 10G — ECS4100-28T/ECS5512 uplinks ───────────────────────────
  { pid: "ECS-SFP-10G-SR",   formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 300,   reachLabel: "SR",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  standard: "10GBASE-SR",  notes: "EnGenius 10G SFP+ 850nm 300m" },
  { pid: "ECS-SFP-10G-LR",   formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 10000, reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "10GBASE-LR",  notes: "EnGenius 10G SFP+ 1310nm 10km" },
  { pid: "ECS-SFP-10G-ER",   formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 40000, reachLabel: "ER",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", standard: "10GBASE-ER",  notes: "EnGenius 10G SFP+ 1550nm 40km" },
  { pid: "ECS-SFP-10G-BXU20",formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 20000, reachLabel: "BX-U", fiberType: "SMF", connector: "LC",  wavelengths: "1270nm TX / 1330nm RX", notes: "EnGenius 10G BiDi SFP+ Tx1270 20km" },
  { pid: "ECS-SFP-10G-BXD20",formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 20000, reachLabel: "BX-D", fiberType: "SMF", connector: "LC",  wavelengths: "1330nm TX / 1270nm RX", notes: "EnGenius 10G BiDi SFP+ Tx1330 20km" },
  // ── SFP28 25G — ECS5512-54P 25G ports ────────────────────────────────
  { pid: "ECS-SFP28-25G-SR",  formFactor: "SFP28", speedGbps: 25,  speed: "25G",  reachMeters: 100,   reachLabel: "SR",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  standard: "25GBASE-SR",  notes: "EnGenius 25G SFP28 850nm 100m" },
  { pid: "ECS-SFP28-25G-LR",  formFactor: "SFP28", speedGbps: 25,  speed: "25G",  reachMeters: 10000, reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "25GBASE-LR",  notes: "EnGenius 25G SFP28 1310nm 10km" },
  // ── QSFP28 100G — ECS5512-54P core uplinks ───────────────────────────
  { pid: "ECS-QSFP28-100G-SR4",formFactor:"QSFP28",speedGbps: 100, speed: "100G", reachMeters: 100,   reachLabel: "SR4",  fiberType: "MMF", connector: "MPO", wavelengths: "850nm",  standard: "100GBASE-SR4",notes: "EnGenius 100G QSFP28 SR4 MPO 100m" },
  { pid: "ECS-QSFP28-100G-LR4",formFactor:"QSFP28",speedGbps: 100, speed: "100G", reachMeters: 10000, reachLabel: "LR4",  fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", standard: "100GBASE-LR4",notes: "EnGenius 100G QSFP28 LR4 LC 10km" },
  // ── DAC / AOC ─────────────────────────────────────────────────────────
  { pid: "ECS-DAC-10G-1M",    formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 1,     reachLabel: "DAC",  fiberType: "DAC", connector: "DAC",                        notes: "EnGenius 10G SFP+ DAC twinax 1m" },
  { pid: "ECS-DAC-10G-3M",    formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 3,     reachLabel: "DAC",  fiberType: "DAC", connector: "DAC",                        notes: "EnGenius 10G SFP+ DAC twinax 3m" },
  { pid: "ECS-AOC-10G-10M",   formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 10,    reachLabel: "AOC",  fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  notes: "EnGenius 10G SFP+ AOC active optical 10m" },
];

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

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

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

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

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