/**
 * Sierra Wireless OEM Transceiver Catalog Seed
 *
 * Sierra Wireless (acquired by Semtech in 2023) manufactures industrial IoT
 * routers and gateways. Their RV55, RV50X, LX60, LX40, and RX55 platforms
 * include SFP/SFP+ cages for WAN uplinks in harsh industrial environments.
 * Extended-temp industrial variants cover -40°C to +85°C operation.
 *
 * Sources:
 *  - Sierra Wireless RV55 Industrial Router product selector
 *  - Sierra Wireless LX60/LX40 SFP compatibility guide
 *  - Semtech (Sierra) accessories catalog 2023
 *
 * Run:  tsx packages/scraper/src/scrapers/sierra-wireless-oem.ts
 * Cron: daily at 03:30
 */

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

interface SierraPID {
  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 SIERRA_PIDS: SierraPID[] = [
  // ── 1G SFP — RV50X / RV55 WAN uplinks ───────────────────────────────
  { pid: "SW-SFP-1G-SX",      formFactor: "SFP",  speedGbps: 1,  speed: "1G",  reachMeters: 550,   reachLabel: "SX",   fiberType: "MMF", connector: "LC",   wavelengths: "850nm",  standard: "1000BASE-SX", category: "DataCenter", notes: "Sierra Wireless 1G SX SFP for RV-series routers" },
  { pid: "SW-SFP-1G-LX",      formFactor: "SFP",  speedGbps: 1,  speed: "1G",  reachMeters: 10000, reachLabel: "LX",   fiberType: "SMF", connector: "LC",   wavelengths: "1310nm", standard: "1000BASE-LX", category: "DataCenter", notes: "Sierra Wireless 1G LX SFP 10km" },
  { pid: "SW-SFP-1G-LX-E",    formFactor: "SFP",  speedGbps: 1,  speed: "1G",  reachMeters: 40000, reachLabel: "EX",   fiberType: "SMF", connector: "LC",   wavelengths: "1310nm", standard: "1000BASE-EX", category: "DataCenter", notes: "Sierra Wireless 1G SFP 40km extended reach" },
  { pid: "SW-SFP-1G-ZX",      formFactor: "SFP",  speedGbps: 1,  speed: "1G",  reachMeters: 80000, reachLabel: "ZX",   fiberType: "SMF", connector: "LC",   wavelengths: "1550nm", standard: "1000BASE-ZX", category: "DataCenter", notes: "Sierra Wireless 1G ZX SFP 80km" },
  { pid: "SW-SFP-1G-T",       formFactor: "SFP",  speedGbps: 1,  speed: "1G",  reachMeters: 100,   reachLabel: "T",    fiberType: "CU",  connector: "RJ-45",                       standard: "1000BASE-T",  category: "DataCenter", notes: "Sierra Wireless 1G copper SFP RJ45" },
  { pid: "SW-SFP-1G-BX-U",    formFactor: "SFP",  speedGbps: 1,  speed: "1G",  reachMeters: 20000, reachLabel: "BX-U", fiberType: "SMF", connector: "LC",   wavelengths: "1310nm TX / 1550nm RX", category: "DataCenter", notes: "Sierra Wireless 1G BiDi SFP Tx1310 upstream" },
  { pid: "SW-SFP-1G-BX-D",    formFactor: "SFP",  speedGbps: 1,  speed: "1G",  reachMeters: 20000, reachLabel: "BX-D", fiberType: "SMF", connector: "LC",   wavelengths: "1550nm TX / 1310nm RX", category: "DataCenter", notes: "Sierra Wireless 1G BiDi SFP Tx1550 downstream" },
  // ── 10G SFP+ — LX60 / RX55 aggregation ──────────────────────────────
  { pid: "SW-SFP-10G-SR",     formFactor: "SFP+", speedGbps: 10, speed: "10G", reachMeters: 300,   reachLabel: "SR",   fiberType: "MMF", connector: "LC",   wavelengths: "850nm",  standard: "10GBASE-SR",  category: "DataCenter", notes: "Sierra Wireless 10G SR SFP+" },
  { pid: "SW-SFP-10G-LR",     formFactor: "SFP+", speedGbps: 10, speed: "10G", reachMeters: 10000, reachLabel: "LR",   fiberType: "SMF", connector: "LC",   wavelengths: "1310nm", standard: "10GBASE-LR",  category: "DataCenter", notes: "Sierra Wireless 10G LR SFP+" },
  { pid: "SW-SFP-10G-ER",     formFactor: "SFP+", speedGbps: 10, speed: "10G", reachMeters: 40000, reachLabel: "ER",   fiberType: "SMF", connector: "LC",   wavelengths: "1550nm", standard: "10GBASE-ER",  category: "DataCenter", notes: "Sierra Wireless 10G ER SFP+ 40km" },
  // ── Industrial extended-temp variants (-40°C to +85°C) ──────────────
  { pid: "SW-SFP-1G-SX-IND",  formFactor: "SFP",  speedGbps: 1,  speed: "1G",  reachMeters: 550,   reachLabel: "SX",   fiberType: "MMF", connector: "LC",   wavelengths: "850nm",  standard: "1000BASE-SX", category: "Industrial", notes: "Sierra Wireless 1G SFP Industrial -40~85°C" },
  { pid: "SW-SFP-1G-LX-IND",  formFactor: "SFP",  speedGbps: 1,  speed: "1G",  reachMeters: 10000, reachLabel: "LX",   fiberType: "SMF", connector: "LC",   wavelengths: "1310nm", standard: "1000BASE-LX", category: "Industrial", notes: "Sierra Wireless 1G LX SFP Industrial grade" },
  { pid: "SW-SFP-10G-SR-IND", formFactor: "SFP+", speedGbps: 10, speed: "10G", reachMeters: 300,   reachLabel: "SR",   fiberType: "MMF", connector: "LC",   wavelengths: "850nm",  standard: "10GBASE-SR",  category: "Industrial", notes: "Sierra Wireless 10G SR SFP+ Industrial" },
  { pid: "SW-SFP-10G-LR-IND", formFactor: "SFP+", speedGbps: 10, speed: "10G", reachMeters: 10000, reachLabel: "LR",   fiberType: "SMF", connector: "LC",   wavelengths: "1310nm", standard: "10GBASE-LR",  category: "Industrial", notes: "Sierra Wireless 10G LR SFP+ Industrial" },
  // ── DAC twinax ───────────────────────────────────────────────────────
  { pid: "SW-DAC-SFP+-1M",    formFactor: "SFP+", speedGbps: 10, speed: "10G", reachMeters: 1,     reachLabel: "DAC",  fiberType: "DAC", connector: "DAC",                         category: "DataCenter", notes: "Sierra Wireless 10G SFP+ DAC 1m" },
  { pid: "SW-DAC-SFP+-3M",    formFactor: "SFP+", speedGbps: 10, speed: "10G", reachMeters: 3,     reachLabel: "DAC",  fiberType: "DAC", connector: "DAC",                         category: "DataCenter", notes: "Sierra Wireless 10G SFP+ DAC 3m" },
  // ── QSFP+ — RX55 high-density aggregation ───────────────────────────
  { pid: "SW-QSFP-40G-SR4",   formFactor: "QSFP+",speedGbps: 40, speed: "40G", reachMeters: 150,   reachLabel: "SR4",  fiberType: "MMF", connector: "MPO",  wavelengths: "850nm",  standard: "40GBASE-SR4", category: "DataCenter", notes: "Sierra Wireless 40G QSFP+ SR4 MPO" },
  { pid: "SW-QSFP-40G-LR4",   formFactor: "QSFP+",speedGbps: 40, speed: "40G", reachMeters: 10000, reachLabel: "LR4",  fiberType: "SMF", connector: "LC",   wavelengths: "1310nm", standard: "40GBASE-LR4", category: "DataCenter", notes: "Sierra Wireless 40G QSFP+ LR4 LC" },
];

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

  const vendorId = await ensureVendor(
    "Sierra Wireless",
    "oem",
    "https://www.sierrawireless.com",
    undefined
  );

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

  for (const p of SIERRA_PIDS) {
    const slug = `sierra-wireless-${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',$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),
           category     = EXCLUDED.category,
           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.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=== Sierra Wireless OEM Seed Complete ===`);
  console.log(`  Inserted: ${inserted}, Updated: ${updated}, Errors: ${errors}`);
  console.log(`  Total PIDs: ${SIERRA_PIDS.length}\n`);
}

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