/**
 * Nokia AirScale OEM Transceiver Catalog Seed
 *
 * Seeds Nokia AirScale-branded transceiver PIDs used in 5G/LTE baseband
 * and radio unit fronthaul deployments (CPRI and eCPRI optical interfaces
 * for AirScale Radio Access products: AirScale Base Station, ABIA/ABIA-R,
 * ASBA, ASIK baseband units and AEHF/AEQF/AEFZ remote radio heads).
 *
 * CPRI option mapping:
 *   Option 1: 614.4 Mbps  |  Option 2: 1228.8 Mbps
 *   Option 3: 2457.6 Mbps |  Option 4: 4915.2 Mbps
 *   Option 5: 9830.4 Mbps |  Option 7: 10137.6 Mbps (CPRI opt 7 / eCPRI)
 *
 * Sources:
 *  - Nokia AirScale Radio Access product portfolio (nokia.com)
 *  - CPRI Specification v7.0
 *  - eCPRI Specification v2.0
 *
 * Run:  tsx packages/scraper/src/scrapers/nokia-airscale-oem.ts
 * Cron: daily at 09:30
 */

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

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

const NOKIA_AIRSCALE_PIDS: NokiaAirscalePID[] = [
  // ── CPRI Option 1/2 — 1G SFP ────────────────────────────────────────────
  { pid: "NAS-SFP-CPRI-1G-SX",      formFactor: "SFP",   speedGbps: 1,      speed: "1G",   reachMeters: 300,    reachLabel: "SX",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",       standard: "1000BASE-SX",  notes: "Nokia AirScale CPRI option 1 (614.4 Mbps) / option 2 (1228.8 Mbps) SFP SX fronthaul" },
  { pid: "NAS-SFP-CPRI-2G-LX",      formFactor: "SFP",   speedGbps: 1.2288, speed: "1.2G", reachMeters: 10000,  reachLabel: "LX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",      standard: "1000BASE-LX",  notes: "Nokia AirScale CPRI option 2 (1228.8 Mbps) SFP LX fronthaul" },

  // ── CPRI Option 7 — 10G SFP+ ────────────────────────────────────────────
  { pid: "NAS-SFP-CPRI-10G-SR",     formFactor: "SFP+",  speedGbps: 10.137, speed: "10G",  reachMeters: 300,    reachLabel: "SR",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",       standard: "10GBASE-SR",   notes: "Nokia AirScale CPRI option 7 (10137.6 Mbps) SFP+ SR — standard LTE/5G fronthaul" },
  { pid: "NAS-SFP-CPRI-10G-LR",     formFactor: "SFP+",  speedGbps: 10.137, speed: "10G",  reachMeters: 10000,  reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",      standard: "10GBASE-LR",   notes: "Nokia AirScale CPRI option 7 SFP+ LR for long-reach fronthaul (up to 10 km)" },

  // ── CPRI Option 7 — 40G QSFP+ ───────────────────────────────────────────
  { pid: "NAS-QSFP-CPRI-40G-SR4",   formFactor: "QSFP+", speedGbps: 40,     speed: "40G",  reachMeters: 150,    reachLabel: "SR4",  fiberType: "MMF", connector: "MPO", wavelengths: "850nm",       standard: "40GBASE-SR4",  notes: "Nokia AirScale aggregated CPRI 40G QSFP+ SR4 (4x CPRI opt 7 lanes)" },

  // ── eCPRI — 25G SFP28 ───────────────────────────────────────────────────
  { pid: "NAS-SFP28-ECPRI-25G-SR",  formFactor: "SFP28", speedGbps: 25,     speed: "25G",  reachMeters: 100,    reachLabel: "SR",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",       standard: "25GBASE-SR",   notes: "Nokia AirScale eCPRI 25G SFP28 SR for 5G NR O-RAN fronthaul (Option 7-2x split)" },
  { pid: "NAS-SFP28-ECPRI-25G-LR",  formFactor: "SFP28", speedGbps: 25,     speed: "25G",  reachMeters: 10000,  reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",      standard: "25GBASE-LR",   notes: "Nokia AirScale eCPRI 25G SFP28 LR for long-reach 5G NR fronthaul" },

  // ── eCPRI — 100G QSFP28 ─────────────────────────────────────────────────
  { pid: "NAS-QSFP28-ECPRI-100G-SR4", formFactor: "QSFP28", speedGbps: 100, speed: "100G", reachMeters: 100,    reachLabel: "SR4",  fiberType: "MMF", connector: "MPO", wavelengths: "850nm",       standard: "100GBASE-SR4", notes: "Nokia AirScale eCPRI 100G QSFP28 SR4 for massive MIMO aggregated fronthaul" },

  // ── Standard data-plane SFP+ ────────────────────────────────────────────
  { pid: "NAS-SFP-10G-SR",          formFactor: "SFP+",  speedGbps: 10,     speed: "10G",  reachMeters: 300,    reachLabel: "SR",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",       standard: "10GBASE-SR",   notes: "Nokia AirScale standard 10G SFP+ SR for BBU uplink / transport" },
  { pid: "NAS-SFP-10G-LR",          formFactor: "SFP+",  speedGbps: 10,     speed: "10G",  reachMeters: 10000,  reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",      standard: "10GBASE-LR" },

  // ── 40G QSFP+ data-plane ────────────────────────────────────────────────
  { pid: "NAS-QSFP-40G-LR4",        formFactor: "QSFP+", speedGbps: 40,     speed: "40G",  reachMeters: 10000,  reachLabel: "LR4",  fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",      standard: "40GBASE-LR4" },

  // ── 100G QSFP28 data-plane ──────────────────────────────────────────────
  { pid: "NAS-QSFP-100G-LR4",       formFactor: "QSFP28", speedGbps: 100,   speed: "100G", reachMeters: 10000,  reachLabel: "LR4",  fiberType: "SMF", connector: "LC",  wavelengths: "1295-1310nm", standard: "100GBASE-LR4", notes: "Nokia AirScale 100G QSFP28 LR4 for midhaul / backhaul" },

  // ── 1G SFP management ───────────────────────────────────────────────────
  { pid: "NAS-SFP-GE-SX",           formFactor: "SFP",   speedGbps: 1,      speed: "1G",   reachMeters: 550,    reachLabel: "SX",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",       standard: "1000BASE-SX" },
  { pid: "NAS-SFP-GE-LX",           formFactor: "SFP",   speedGbps: 1,      speed: "1G",   reachMeters: 10000,  reachLabel: "LX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",      standard: "1000BASE-LX" },

  // ── BiDi SFP+ (single-fiber fronthaul) ──────────────────────────────────
  { pid: "NAS-SFP-10G-BIDI-1270",   formFactor: "SFP+",  speedGbps: 10,     speed: "10G",  reachMeters: 20000,  reachLabel: "BIDI", fiberType: "SMF", connector: "LC",  wavelengths: "1270/1330nm",                            notes: "Nokia AirScale BiDi SFP+ Tx1270/Rx1330 for CPRI opt 7 single-fiber fronthaul" },
  { pid: "NAS-SFP-10G-BIDI-1330",   formFactor: "SFP+",  speedGbps: 10,     speed: "10G",  reachMeters: 20000,  reachLabel: "BIDI", fiberType: "SMF", connector: "LC",  wavelengths: "1330/1270nm",                            notes: "Nokia AirScale BiDi SFP+ Tx1330/Rx1270 (paired with NAS-SFP-10G-BIDI-1270)" },
];

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

  const vendorId = await ensureVendor(
    "Nokia AirScale",
    "oem",
    "https://www.nokia.com/networks/mobile-networks/airscale-radio-access/",
    undefined
  );

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

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

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