/**
 * Tejas Networks OEM Transceiver Catalog Seed
 *
 * Seeds Tejas Networks-branded transceiver PIDs for OTN, MPLS-TP, and
 * packet-optical transport platforms (TJ1400, TJ1600, TJ100 series).
 * Tejas Networks is an Indian telecom equipment vendor, now a Tata Group company.
 *
 * Sources:
 *  - Tejas Networks TJ1400/TJ1600 Optical Module Compatibility List (tejasnetworks.com)
 *  - Tejas Networks OTN Platform Optics Portfolio
 *  - Tejas Networks CWDM/DWDM Module Guide
 *
 * Run:  tsx packages/scraper/src/scrapers/tejas-networks-oem.ts
 * Cron: daily at 00:00
 */

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

interface TejasNetworksPID {
  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 TEJAS_NETWORKS_PIDS: TejasNetworksPID[] = [
  // ── 1G SFP ───────────────────────────────────────────────────────────────
  { pid: "SFP-1G-SX",       formFactor: "SFP",     speedGbps: 1,    speed: "1G",    reachMeters: 550,     reachLabel: "SX",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",           standard: "1000BASE-SX" },
  { pid: "SFP-1G-LX",       formFactor: "SFP",     speedGbps: 1,    speed: "1G",    reachMeters: 10000,   reachLabel: "LX",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",          standard: "1000BASE-LX" },

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

  // ── SONET/SDH SFP ─────────────────────────────────────────────────────────
  { pid: "SFP-OC3-SR",      formFactor: "SFP",     speedGbps: 0.155,speed: "OC3",   reachMeters: 2000,    reachLabel: "SR",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",           category: "Telecom", notes: "Tejas OC-3/STM-1 155M SFP SR" },
  { pid: "SFP-OC3-LR",      formFactor: "SFP",     speedGbps: 0.155,speed: "OC3",   reachMeters: 40000,   reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",          category: "Telecom", notes: "Tejas OC-3/STM-1 155M SFP LR" },
  { pid: "SFP-OC12-SR",     formFactor: "SFP",     speedGbps: 0.622,speed: "OC12",  reachMeters: 2000,    reachLabel: "SR",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",           category: "Telecom", notes: "Tejas OC-12/STM-4 622M SFP SR" },
  { pid: "SFP-OC12-LR",     formFactor: "SFP",     speedGbps: 0.622,speed: "OC12",  reachMeters: 40000,   reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",          category: "Telecom", notes: "Tejas OC-12/STM-4 622M SFP LR" },
  { pid: "SFP-OC48-SR",     formFactor: "SFP",     speedGbps: 2.5,  speed: "OC48",  reachMeters: 2000,    reachLabel: "SR",   fiberType: "MMF", connector: "LC",  wavelengths: "850nm",           category: "Telecom", notes: "Tejas OC-48/STM-16 2.488G SFP SR" },

  // ── OTN SFP+ ──────────────────────────────────────────────────────────────
  { pid: "SFP-OTU1-GE",     formFactor: "SFP",     speedGbps: 1.25, speed: "OTU1",  reachMeters: 10000,   reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",          category: "Telecom", notes: "Tejas OTU1 GbE mapped SFP for OTN transport" },
  { pid: "SFP-OTU2-10G",    formFactor: "SFP+",    speedGbps: 10,   speed: "OTU2",  reachMeters: 40000,   reachLabel: "LR",   fiberType: "SMF", connector: "LC",  wavelengths: "1310nm",          category: "Telecom", notes: "Tejas OTU2 10.709G SFP+ for OTN transport" },

  // ── CWDM SFP ──────────────────────────────────────────────────────────────
  { pid: "CWDM-SFP-1550",   formFactor: "SFP",     speedGbps: 1,    speed: "1G",    reachMeters: 60000,   reachLabel: "CWDM", fiberType: "SMF", connector: "LC",  wavelengths: "1550nm",          category: "Telecom", notes: "Tejas CWDM SFP 1550nm for metro/access" },

  // ── DWDM SFP+ ─────────────────────────────────────────────────────────────
  { pid: "DWDM-SFP10G-C",   formFactor: "SFP+",    speedGbps: 10,   speed: "10G",   reachMeters: 80000,   reachLabel: "DWDM", fiberType: "SMF", connector: "LC",  wavelengths: "C-band ITU DWDM", category: "Telecom", notes: "Tejas DWDM 10G SFP+ C-band for OTN line side" },

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

  // ── 100G QSFP28 / CFP ─────────────────────────────────────────────────────
  { pid: "QSFP-100G-LR4",   formFactor: "QSFP28",  speedGbps: 100,  speed: "100G",  reachMeters: 10000,   reachLabel: "LR4",  fiberType: "SMF", connector: "LC",  wavelengths: "1295-1310nm",     standard: "100GBASE-LR4" },
  { pid: "CFP-100G-LR4",    formFactor: "CFP",      speedGbps: 100,  speed: "100G",  reachMeters: 10000,   reachLabel: "LR4",  fiberType: "SMF", connector: "LC",  wavelengths: "1295-1310nm",     category: "Telecom", notes: "Tejas 100G CFP LR4 for OTN/MPLS-TP line cards" },
];

// PIDs that use 'Telecom' category (SONET/SDH, OTN, CWDM, DWDM, CFP line-side)
const TELECOM_PIDS = new Set([
  "SFP-OC3-SR",
  "SFP-OC3-LR",
  "SFP-OC12-SR",
  "SFP-OC12-LR",
  "SFP-OC48-SR",
  "SFP-OTU1-GE",
  "SFP-OTU2-10G",
  "CWDM-SFP-1550",
  "DWDM-SFP10G-C",
  "CFP-100G-LR4",
]);

export async function scrapeTejasNetworksOem(): Promise<void> {
  console.log("=== Tejas Networks OEM Transceiver Seed ===\n");

  const vendorId = await ensureVendor(
    "Tejas Networks",
    "oem",
    "https://www.tejasnetworks.com",
    undefined
  );

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

  for (const p of TEJAS_NETWORKS_PIDS) {
    const slug     = `tejas-${p.pid.toLowerCase().replace(/[^a-z0-9]+/g, "-")}`;
    const category = p.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=== Tejas Networks OEM Seed Complete ===`);
  console.log(`  Inserted: ${inserted}`);
  console.log(`  Updated:  ${updated}`);
  console.log(`  Errors:   ${errors}`);
  console.log(`  Total PIDs: ${TEJAS_NETWORKS_PIDS.length}\n`);
}

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