/**
 * Inphi Corporation OEM Transceiver Catalog Seed
 *
 * Seeds Inphi (acquired by Marvell in 2021) branded optical transceiver PIDs.
 * Inphi pioneered the ColorZ DWDM pluggable platform and COLORZ II 400G
 * coherent modules, enabling DWDM over standard QSFP28/QSFP-DD form factors
 * for metro and DCI applications.
 *
 * Sources:
 *  - Inphi ColorZ 100G DWDM QSFP28 datasheet
 *  - Inphi COLORZ II 400G QSFP-DD product brief
 *  - Inphi Canopus 200G CFP2-DCO coherent specs
 *
 * Run:  tsx packages/scraper/src/scrapers/inphi-oem.ts
 * Cron: daily at 03:15
 */

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

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

const INPHI_PIDS: InphiPID[] = [
  // ── ColorZ 100G DWDM QSFP28 ──────────────────────────────────────────
  { pid: "COLORZ-100G-CH17",    formFactor: "QSFP28", speedGbps: 100, speed: "100G", reachMeters: 80000,   reachLabel: "DWDM-C17", fiberType: "SMF", connector: "LC", wavelengths: "1563.86nm (C17)", notes: "Inphi ColorZ 100G Ch17 DWDM QSFP28" },
  { pid: "COLORZ-100G-CH21",    formFactor: "QSFP28", speedGbps: 100, speed: "100G", reachMeters: 80000,   reachLabel: "DWDM-C21", fiberType: "SMF", connector: "LC", wavelengths: "1560.61nm (C21)", notes: "Inphi ColorZ 100G Ch21" },
  { pid: "COLORZ-100G-CH33",    formFactor: "QSFP28", speedGbps: 100, speed: "100G", reachMeters: 80000,   reachLabel: "DWDM-C33", fiberType: "SMF", connector: "LC", wavelengths: "1550.92nm (C33)", notes: "Inphi ColorZ 100G Ch33" },
  { pid: "COLORZ-100G-TUNE",    formFactor: "QSFP28", speedGbps: 100, speed: "100G", reachMeters: 80000,   reachLabel: "DWDM-TUNE",fiberType: "SMF", connector: "LC", wavelengths: "C-band tunable",  notes: "Inphi ColorZ 100G Tunable QSFP28 — landmark DCI product" },

  // ── ColorZ II 400G DWDM QSFP-DD ──────────────────────────────────────
  { pid: "COLORZII-400G-ZR",    formFactor: "QSFP-DD",speedGbps: 400, speed: "400G", reachMeters: 120000,  reachLabel: "ZR",       fiberType: "SMF", connector: "LC", wavelengths: "C-band", standard: "400ZR", notes: "Inphi COLORZ II 400G ZR QSFP-DD" },
  { pid: "COLORZII-400G-ZRP",   formFactor: "QSFP-DD",speedGbps: 400, speed: "400G", reachMeters: 1000000, reachLabel: "ZR+",      fiberType: "SMF", connector: "LC", wavelengths: "C-band",             notes: "Inphi COLORZ II 400G ZR+ extended reach" },
  { pid: "COLORZII-400G-TUNE",  formFactor: "QSFP-DD",speedGbps: 400, speed: "400G", reachMeters: 80000,   reachLabel: "DWDM-TUNE",fiberType: "SMF", connector: "LC", wavelengths: "C-band tunable",     notes: "Inphi COLORZ II 400G tunable" },

  // ── Canopus 200G CFP2-DCO ─────────────────────────────────────────────
  { pid: "CANOPUS-200G-CFP2",   formFactor: "CFP2-DCO",speedGbps: 200,speed: "200G", reachMeters: 1000000, reachLabel: "LH",       fiberType: "SMF", connector: "LC", wavelengths: "C-band", notes: "Inphi Canopus 200G CFP2-DCO coherent" },

  // ── Standard non-coherent (Inphi silicon photonics) ──────────────────
  { pid: "INPHI-QSFP28-100G-SR4", formFactor: "QSFP28",speedGbps: 100,speed: "100G",reachMeters: 100,     reachLabel: "SR4",      fiberType: "MMF", connector: "MPO", wavelengths: "850nm",       standard: "100GBASE-SR4" },
  { pid: "INPHI-QSFP28-100G-LR4", formFactor: "QSFP28",speedGbps: 100,speed: "100G",reachMeters: 10000,   reachLabel: "LR4",      fiberType: "SMF", connector: "LC",  wavelengths: "1295-1310nm", standard: "100GBASE-LR4" },
  { pid: "INPHI-QSFPDD-400G-DR4", formFactor: "QSFP-DD",speedGbps: 400,speed: "400G",reachMeters: 500,    reachLabel: "DR4",      fiberType: "SMF", connector: "MPO", wavelengths: "1310nm",      standard: "400GBASE-DR4" },
  { pid: "INPHI-QSFPDD-400G-FR4", formFactor: "QSFP-DD",speedGbps: 400,speed: "400G",reachMeters: 2000,   reachLabel: "FR4",      fiberType: "SMF", connector: "LC",  wavelengths: "1271-1331nm", standard: "400GBASE-FR4" },
  { pid: "INPHI-OSFP-800G-SR8",   formFactor: "OSFP",   speedGbps: 800,speed: "800G",reachMeters: 100,    reachLabel: "SR8",      fiberType: "MMF", connector: "MPO", wavelengths: "850nm",       standard: "800GBASE-SR8", notes: "Inphi/Marvell 800G SR8" },
];

export async function scrapeInphiOem(): Promise<void> {
  console.log("=== Inphi (Marvell) OEM Transceiver Seed ===\n");

  const vendorId = await ensureVendor(
    "Inphi (Marvell)",
    "oem",
    "https://www.marvell.com",
    undefined
  );

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

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

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