/**
 * ZTE Access OEM Transceiver Catalog Seed
 *
 * Seeds ZTE Access-branded transceiver PIDs for C600 / C650 / C680 OLT
 * platforms supporting GPON, XGS-PON, and 10G-EPON line cards.
 *
 * Sources:
 *  - ZTE Fixed Network Access OLT Product Page (zte.com.cn/global)
 *  - ZTE C650 / C680 OLT Hardware Description
 *  - ZTE XGS-PON Optical Module Data Sheets
 *
 * Run:  tsx packages/scraper/src/scrapers/zte-access-oem.ts
 * Cron: daily at 08:45
 */

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

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

const TELECOM_PIDS: Set<string> = new Set([
  "ZTE-GPON-OLT-B",
  "ZTE-GPON-OLT-C",
  "ZTE-XGSPON-OLT-1577",
  "ZTE-XGSPON-ONU-1270",
  "ZTE-EPON-OLT",
  "ZTE-10G-EPON-OLT",
  "ZTE-SFP-CWDM-1550",
  "ZTE-SFP-BIDI-1310",
  "ZTE-SFP-BIDI-1490",
  "ZTE-SFP-10G-BIDI-1270",
]);

const ZTE_ACCESS_PIDS: ZteAccessPID[] = [
  // ── GPON OLT SFP ─────────────────────────────────────────────────────────
  { pid: "ZTE-GPON-OLT-B",        formFactor: "SFP",    speedGbps: 2.488, speed: "GPON",      reachMeters: 20000, reachLabel: "GPON-20K",    fiberType: "SMF", connector: "SC",  wavelengths: "1490/1310nm", standard: "G.984",    notes: "ZTE Access GPON OLT SFP Class B+, C600/C650 platform",   isTelecom: true },
  { pid: "ZTE-GPON-OLT-C",        formFactor: "SFP",    speedGbps: 2.488, speed: "GPON",      reachMeters: 20000, reachLabel: "GPON-C+",     fiberType: "SMF", connector: "SC",  wavelengths: "1490/1310nm", standard: "G.984",    notes: "ZTE Access GPON OLT SFP Class C+, extended reach",        isTelecom: true },

  // ── XGS-PON OLT / ONU SFP+ ───────────────────────────────────────────────
  { pid: "ZTE-XGSPON-OLT-1577",   formFactor: "SFP+",   speedGbps: 10,    speed: "XGS-PON",   reachMeters: 20000, reachLabel: "XGS-PON-20K", fiberType: "SMF", connector: "SC",  wavelengths: "1577/1270nm", standard: "G.9807.1", notes: "ZTE Access XGS-PON OLT SFP+ for C650/C680",              isTelecom: true },
  { pid: "ZTE-XGSPON-ONU-1270",   formFactor: "SFP+",   speedGbps: 10,    speed: "XGS-PON",   reachMeters: 20000, reachLabel: "XGS-ONU-20K", fiberType: "SMF", connector: "SC",  wavelengths: "1270/1577nm", standard: "G.9807.1", notes: "ZTE Access XGS-PON ONU SFP+ 1270nm TX",                  isTelecom: true },

  // ── EPON OLT SFP ─────────────────────────────────────────────────────────
  { pid: "ZTE-EPON-OLT",          formFactor: "SFP",    speedGbps: 1,     speed: "EPON",      reachMeters: 20000, reachLabel: "EPON-20K",    fiberType: "SMF", connector: "SC",  wavelengths: "1490/1310nm", standard: "802.3ah",  notes: "ZTE Access EPON OLT SFP 1G/1G",                          isTelecom: true },
  { pid: "ZTE-10G-EPON-OLT",      formFactor: "SFP+",   speedGbps: 10,    speed: "10G-EPON",  reachMeters: 20000, reachLabel: "10G-EPON-20K",fiberType: "SMF", connector: "SC",  wavelengths: "1577/1270nm", standard: "802.3av",  notes: "ZTE Access 10G-EPON OLT SFP+",                           isTelecom: true },

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

  // ── Standard 10G SFP+ ────────────────────────────────────────────────────
  { pid: "ZTE-SFP-10G-SR",        formFactor: "SFP+",   speedGbps: 10,    speed: "10G",       reachMeters: 300,   reachLabel: "SR",          fiberType: "MMF", connector: "LC",  wavelengths: "850nm",       standard: "10GBASE-SR" },
  { pid: "ZTE-SFP-10G-LR",        formFactor: "SFP+",   speedGbps: 10,    speed: "10G",       reachMeters: 10000, reachLabel: "LR",          fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",      standard: "10GBASE-LR" },
  { pid: "ZTE-SFP-10G-ER",        formFactor: "SFP+",   speedGbps: 10,    speed: "10G",       reachMeters: 40000, reachLabel: "ER",          fiberType: "SMF", connector: "LC",  wavelengths: "1550nm",      standard: "10GBASE-ER" },

  // ── CWDM SFP ─────────────────────────────────────────────────────────────
  { pid: "ZTE-SFP-CWDM-1550",     formFactor: "SFP",    speedGbps: 1,     speed: "1G",        reachMeters: 40000, reachLabel: "CWDM-1550",   fiberType: "SMF", connector: "LC",  wavelengths: "1550nm",      standard: "CWDM",     notes: "ZTE Access CWDM SFP 1550nm",                             isTelecom: true },

  // ── BiDi SFP ─────────────────────────────────────────────────────────────
  { pid: "ZTE-SFP-BIDI-1310",     formFactor: "SFP",    speedGbps: 1,     speed: "1G",        reachMeters: 20000, reachLabel: "BiDi-1310",   fiberType: "SMF", connector: "SC",  wavelengths: "1310/1490nm",                         notes: "ZTE Access 1G BiDi SFP 1310nm TX / 1490nm RX",           isTelecom: true },
  { pid: "ZTE-SFP-BIDI-1490",     formFactor: "SFP",    speedGbps: 1,     speed: "1G",        reachMeters: 20000, reachLabel: "BiDi-1490",   fiberType: "SMF", connector: "SC",  wavelengths: "1490/1310nm",                         notes: "ZTE Access 1G BiDi SFP 1490nm TX / 1310nm RX",           isTelecom: true },
  { pid: "ZTE-SFP-10G-BIDI-1270", formFactor: "SFP+",   speedGbps: 10,    speed: "10G",       reachMeters: 20000, reachLabel: "BiDi-1270",   fiberType: "SMF", connector: "LC",  wavelengths: "1270/1330nm",                         notes: "ZTE Access 10G BiDi SFP+ 1270nm TX",                     isTelecom: true },

  // ── 25G SFP28 ────────────────────────────────────────────────────────────
  { pid: "ZTE-SFP-25G-LR",        formFactor: "SFP28",  speedGbps: 25,    speed: "25G",       reachMeters: 10000, reachLabel: "LR",          fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",      standard: "25GBASE-LR" },

  // ── 40G / 100G QSFP ──────────────────────────────────────────────────────
  { pid: "ZTE-QSFP-40G-LR4",      formFactor: "QSFP+",  speedGbps: 40,    speed: "40G",       reachMeters: 10000, reachLabel: "LR4",         fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",      standard: "40GBASE-LR4" },
  { pid: "ZTE-QSFP-100G-LR4",     formFactor: "QSFP28", speedGbps: 100,   speed: "100G",      reachMeters: 10000, reachLabel: "LR4",         fiberType: "SMF", connector: "LC",  wavelengths: "1295-1310nm", standard: "100GBASE-LR4" },
];

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

  const vendorId = await ensureVendor(
    "ZTE Access",
    "oem",
    "https://www.zte.com.cn/global/solutions_latest/fixed/OLT.html",
    undefined
  );

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

  for (const p of ZTE_ACCESS_PIDS) {
    const slug = `zte-access-${p.pid.toLowerCase().replace(/[^a-z0-9]+/g, "-")}`;
    const category = TELECOM_PIDS.has(p.pid) ? "Telecom" : "DataCenter";
    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),
           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, 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=== ZTE Access OEM Seed Complete ===`);
  console.log(`  Inserted: ${inserted}`);
  console.log(`  Updated:  ${updated}`);
  console.log(`  Errors:   ${errors}`);
  console.log(`  Total PIDs: ${ZTE_ACCESS_PIDS.length}\n`);
}

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