/**
 * Ericsson Radio OEM Transceiver Catalog Seed
 *
 * Seeds Ericsson Radio System-branded transceiver PIDs used in CPRI and
 * eCPRI fronthaul deployments for Ericsson RRUS (Remote Radio Unit System),
 * RADIO (AIR series), and baseband (BBU6630/BBU6648/vDU) units.
 *
 * CPRI option / speed mapping:
 *   Option 1/2: 614.4 Mbps   |  Option 3: 1228.8 Mbps
 *   Option 4:   2457.6 Mbps  |  Option 5: 4915.2 Mbps
 *   Option 6:   9830.4 Mbps  |  Option 7: 10137.6 Mbps
 *
 * Sources:
 *  - Ericsson Radio System product portfolio (ericsson.com)
 *  - CPRI Specification v7.0
 *  - eCPRI Specification v2.0
 *  - Ericsson 5G NR fronthaul transport guidelines
 *
 * Run:  tsx packages/scraper/src/scrapers/ericsson-ran-oem.ts
 * Cron: daily at 09:45
 */

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

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

const ERICSSON_RAN_PIDS: EricssonRanPID[] = [
  // ── CPRI Option 1/2 — 614.4 Mbps ────────────────────────────────────────
  { pid: "ER-SFP-CPRI-614M",        formFactor: "SFP",   speedGbps: 0.6144, speed: "0.6G", reachMeters: 10000,  reachLabel: "LX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",      standard: "1000BASE-LX",  notes: "Ericsson RRUS CPRI option 1 (614.4 Mbps) / option 2 SFP — narrow-band LTE remote radio" },

  // ── CPRI Option 3 — 1228.8 Mbps ─────────────────────────────────────────
  { pid: "ER-SFP-CPRI-1228M",       formFactor: "SFP",   speedGbps: 1.2288, speed: "1.2G", reachMeters: 10000,  reachLabel: "LX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",      standard: "1000BASE-LX",  notes: "Ericsson RRUS CPRI option 3 (1228.8 Mbps) SFP — 2x10 MHz LTE carrier fronthaul" },

  // ── CPRI Option 4 — 2457.6 Mbps ─────────────────────────────────────────
  { pid: "ER-SFP-CPRI-2457M",       formFactor: "SFP",   speedGbps: 2.4576, speed: "2.5G", reachMeters: 10000,  reachLabel: "LX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",      standard: "OTN",          notes: "Ericsson RRUS CPRI option 4 (2457.6 Mbps) SFP — 4x10 MHz LTE carrier fronthaul" },

  // ── CPRI Option 5 — 4915.2 Mbps ─────────────────────────────────────────
  { pid: "ER-SFP-CPRI-4915M",       formFactor: "SFP",   speedGbps: 4.9152, speed: "4.9G", reachMeters: 10000,  reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",      standard: "OTN",          notes: "Ericsson RRUS CPRI option 5 (4915.2 Mbps) SFP — 4x20 MHz LTE carrier, typical RRU 2T2R" },

  // ── CPRI Option 6 — 9830.4 Mbps ─────────────────────────────────────────
  { pid: "ER-SFP-CPRI-9830M",       formFactor: "SFP+",  speedGbps: 9.83,   speed: "9.8G", reachMeters: 10000,  reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",      standard: "10GBASE-LR",   notes: "Ericsson RRUS CPRI option 6 (9830.4 Mbps) SFP+ — 4T4R LTE or 2T2R 4-carrier fronthaul" },

  // ── CPRI Option 7 — 10137.6 Mbps ────────────────────────────────────────
  { pid: "ER-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: "Ericsson RRUS CPRI option 7 (10137.6 Mbps) SFP+ SR — high-capacity LTE/5G NR fronthaul" },
  { pid: "ER-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: "Ericsson RRUS CPRI option 7 SFP+ LR — long-reach fronthaul up to 10 km" },

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

  // ── eCPRI — 100G QSFP28 ─────────────────────────────────────────────────
  { pid: "ER-QSFP28-ECPRI-100G-SR4", formFactor: "QSFP28", speedGbps: 100,  speed: "100G", reachMeters: 100,    reachLabel: "SR4",  fiberType: "MMF", connector: "MPO", wavelengths: "850nm",       standard: "100GBASE-SR4", notes: "Ericsson AIR eCPRI 100G QSFP28 SR4 for massive MIMO (64T64R) aggregated fronthaul" },

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

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

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

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

  const vendorId = await ensureVendor(
    "Ericsson Radio",
    "oem",
    "https://www.ericsson.com/en/portfolio/networks/ericsson-radio-system",
    undefined
  );

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

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

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