/**
 * Reflex Photonics OEM Transceiver Catalog Seed
 *
 * Reflex Photonics (Quebec, Canada) specializes in ruggedized embedded
 * optical transceivers for aerospace, military, and harsh industrial
 * environments. Their LightABLE series meets MIL-STD-810H (-55°C to +100°C),
 * and their RAD-HARD line targets radiation-tolerant space applications.
 *
 * Sources:
 *  - Reflex Photonics LightABLE SFP/SFP+/QSFP product catalog
 *  - Reflex Photonics RAD-HARD space-grade optics datasheet
 *  - Reflex embedded parallel optical module product brief
 *
 * Run:  tsx packages/scraper/src/scrapers/reflex-photonics-oem.ts
 * Cron: daily at 04:15
 */

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

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

const REFLEX_PIDS: ReflexPID[] = [
  // ── LightABLE SFP 1G — MIL-STD-810H ruggedized ───────────────────────
  { pid: "RFP-SFP-1G-SX-MIL",     formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 300,   reachLabel: "SX",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  notes: "Reflex LightABLE 1G SFP 850nm MIL-STD-810H -55~100°C" },
  { pid: "RFP-SFP-1G-LX-MIL",     formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 10000, reachLabel: "LX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", notes: "Reflex LightABLE 1G SFP 1310nm 10km MIL-grade" },
  { pid: "RFP-SFP-1G-EX-MIL",     formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 40000, reachLabel: "EX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", notes: "Reflex LightABLE 1G SFP 1310nm 40km ruggedized" },
  { pid: "RFP-SFP-1G-BX-MIL",     formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 20000, reachLabel: "BX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm TX / 1550nm RX", notes: "Reflex LightABLE 1G BiDi SFP 20km MIL-grade" },
  // ── LightABLE SFP+ 10G ───────────────────────────────────────────────
  { pid: "RFP-SFP-10G-SR-MIL",    formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 300,   reachLabel: "SR",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",  notes: "Reflex LightABLE 10G SFP+ 850nm 300m ruggedized" },
  { pid: "RFP-SFP-10G-LR-MIL",    formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 10000, reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", notes: "Reflex LightABLE 10G SFP+ 1310nm 10km MIL-grade" },
  { pid: "RFP-SFP-10G-ER-MIL",    formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 40000, reachLabel: "ER",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", notes: "Reflex LightABLE 10G SFP+ 1550nm 40km ruggedized" },
  { pid: "RFP-SFP-10G-ZR-MIL",    formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 80000, reachLabel: "ZR",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm", notes: "Reflex LightABLE 10G SFP+ 1550nm 80km MIL-grade" },
  // ── LightABLE QSFP+ 40G — avionics high-density ──────────────────────
  { pid: "RFP-QSFP-40G-SR4-MIL",  formFactor: "QSFP+", speedGbps: 40,  speed: "40G",  reachMeters: 150,   reachLabel: "SR4",  fiberType: "MMF", connector: "MPO", wavelengths: "850nm",  notes: "Reflex LightABLE 40G QSFP+ SR4 MPO-12 ruggedized" },
  { pid: "RFP-QSFP-40G-LR4-MIL",  formFactor: "QSFP+", speedGbps: 40,  speed: "40G",  reachMeters: 10000, reachLabel: "LR4",  fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", notes: "Reflex LightABLE 40G QSFP+ LR4 10km MIL-grade" },
  // ── LightABLE QSFP28 100G ────────────────────────────────────────────
  { pid: "RFP-QSFP28-100G-SR4-MIL",formFactor:"QSFP28",speedGbps: 100, speed: "100G", reachMeters: 100,   reachLabel: "SR4",  fiberType: "MMF", connector: "MPO", wavelengths: "850nm",  notes: "Reflex LightABLE 100G QSFP28 SR4 MPO MIL-grade" },
  { pid: "RFP-QSFP28-100G-LR4-MIL",formFactor:"QSFP28",speedGbps: 100, speed: "100G", reachMeters: 10000, reachLabel: "LR4",  fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", notes: "Reflex LightABLE 100G QSFP28 LR4 LC ruggedized" },
  // ── Embedded parallel optical — board-level avionics ─────────────────
  { pid: "RFP-EMB-25G-SR-PARL",   formFactor: "SFP28", speedGbps: 25,  speed: "25G",  reachMeters: 100,   reachLabel: "SR",   fiberType: "MMF", connector: "MPO", wavelengths: "850nm",  notes: "Reflex embedded 25G parallel optical avionics module" },
  { pid: "RFP-EMB-100G-4X25-PARL",formFactor: "QSFP28",speedGbps: 100, speed: "100G", reachMeters: 100,   reachLabel: "SR4",  fiberType: "MMF", connector: "MPO", wavelengths: "850nm",  notes: "Reflex embedded 100G 4x25G parallel optical avionics" },
  // ── RAD-HARD space-grade — radiation tolerant ─────────────────────────
  { pid: "RFP-RAD-SFP-1G-LX",     formFactor: "SFP",   speedGbps: 1,   speed: "1G",   reachMeters: 10000, reachLabel: "LX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", notes: "Reflex RAD-HARD 1G SFP 1310nm radiation tolerant space" },
  { pid: "RFP-RAD-SFP-10G-LR",    formFactor: "SFP+",  speedGbps: 10,  speed: "10G",  reachMeters: 10000, reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", notes: "Reflex RAD-HARD 10G SFP+ 1310nm space-grade" },
  { pid: "RFP-RAD-QSFP28-100G-LR4",formFactor:"QSFP28",speedGbps: 100, speed: "100G", reachMeters: 10000, reachLabel: "LR4",  fiberType: "SMF", connector: "LC",  wavelengths: "1310nm", notes: "Reflex RAD-HARD 100G QSFP28 LR4 radiation tolerant" },
];

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

  const vendorId = await ensureVendor(
    "Reflex Photonics",
    "oem",
    "https://www.reflexPhotonics.com",
    undefined
  );

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

  for (const p of REFLEX_PIDS) {
    const slug = `reflex-photonics-${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, market_status, category, notes)
         VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,true,'Mainstream','Industrial',$12)
         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.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=== Reflex Photonics OEM Seed Complete ===`);
  console.log(`  Inserted: ${inserted}, Updated: ${updated}, Errors: ${errors}`);
  console.log(`  Total PIDs: ${REFLEX_PIDS.length}\n`);
}

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