/**
 * Meta OEM Transceiver Catalog Seed
 *
 * Seeds Meta (Facebook)-branded transceiver PIDs used across Meta's global
 * datacenter fleet on the Open Compute Project (OCP) FBOSS switching platform.
 * Meta contributes hardware designs to OCP and uses a META- prefix for optics
 * validated on its internally developed Wedge/Wedge100/Minipack/Montara switch
 * ASICs running FBOSS (Facebook Open Switching System).
 *
 * Notable entries:
 *  - META-QSFP-DD-800G-SR8 / META-OSFP-800G-DR8: Meta's 800G OCP next-gen
 *    fabric ports targeting the Wedge400/Minipack2 upgrade cycle.
 *
 * Sources:
 *  - Meta Engineering networking blog (engineering.fb.com/category/networking-traffic/)
 *  - Open Compute Project networking specifications (opencompute.org)
 *  - OIF 400G-ZR / 800G-FR8 MSA implementation agreements
 *
 * Run:  tsx packages/scraper/src/scrapers/meta-oem.ts
 * Cron: daily at 08:00
 */

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

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

const META_PIDS: MetaPID[] = [
  // ── 1G SFP ───────────────────────────────────────────────────────────────
  {
    pid:         "META-SFP-1G-SX",
    formFactor:  "SFP",
    speedGbps:   1,
    speed:       "1G",
    reachMeters: 550,
    reachLabel:  "SX",
    fiberType:   "MMF",
    connector:   "LC",
    wavelengths: "850nm",
    standard:    "1000BASE-SX",
    notes:       "1GbE SFP SX; Meta OCP server management port; OM2/OM3 MMF up to 550m",
  },
  {
    pid:         "META-SFP-1G-LX",
    formFactor:  "SFP",
    speedGbps:   1,
    speed:       "1G",
    reachMeters: 10000,
    reachLabel:  "LX",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1310nm",
    standard:    "1000BASE-LX",
    notes:       "1GbE SFP LX; Meta OCP server management port; OS2 SMF up to 10km",
  },
  // ── 1G Copper SFP ────────────────────────────────────────────────────────
  {
    pid:         "META-SFP-GE-T",
    formFactor:  "SFP",
    speedGbps:   1,
    speed:       "1G",
    reachMeters: 100,
    reachLabel:  "T",
    fiberType:   "DAC",
    connector:   "RJ45",
    standard:    "1000BASE-T",
    notes:       "1GbE copper SFP RJ45; Meta OCP server management copper option; Cat5e/6",
  },
  // ── 10G SFP+ ─────────────────────────────────────────────────────────────
  {
    pid:         "META-SFP-10G-SR",
    formFactor:  "SFP+",
    speedGbps:   10,
    speed:       "10G",
    reachMeters: 300,
    reachLabel:  "SR",
    fiberType:   "MMF",
    connector:   "LC",
    wavelengths: "850nm",
    standard:    "10GBASE-SR",
    notes:       "10GbE SFP+ SR; Meta FBOSS Wedge100 server downlink; OM3/OM4 MMF up to 300m",
  },
  {
    pid:         "META-SFP-10G-LR",
    formFactor:  "SFP+",
    speedGbps:   10,
    speed:       "10G",
    reachMeters: 10000,
    reachLabel:  "LR",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1310nm",
    standard:    "10GBASE-LR",
    notes:       "10GbE SFP+ LR; Meta FBOSS Wedge100 inter-pod link; OS2 SMF up to 10km",
  },
  {
    pid:         "META-SFP-10G-ER",
    formFactor:  "SFP+",
    speedGbps:   10,
    speed:       "10G",
    reachMeters: 40000,
    reachLabel:  "ER",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1550nm",
    standard:    "10GBASE-ER",
    notes:       "10GbE SFP+ ER; Meta datacenter campus interconnect; OS2 SMF up to 40km",
  },
  // ── 25G SFP28 ────────────────────────────────────────────────────────────
  {
    pid:         "META-SFP28-25G-SR",
    formFactor:  "SFP28",
    speedGbps:   25,
    speed:       "25G",
    reachMeters: 100,
    reachLabel:  "SR",
    fiberType:   "MMF",
    connector:   "LC",
    wavelengths: "850nm",
    standard:    "25GBASE-SR",
    notes:       "25GbE SFP28 SR; Meta OCP server ToR downlink port; OM4 MMF up to 100m",
  },
  {
    pid:         "META-SFP28-25G-LR",
    formFactor:  "SFP28",
    speedGbps:   25,
    speed:       "25G",
    reachMeters: 10000,
    reachLabel:  "LR",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1310nm",
    standard:    "25GBASE-LR",
    notes:       "25GbE SFP28 LR; Meta OCP server ToR downlink port; OS2 SMF up to 10km",
  },
  // ── 40G QSFP+ ────────────────────────────────────────────────────────────
  {
    pid:         "META-QSFP-40G-SR4",
    formFactor:  "QSFP+",
    speedGbps:   40,
    speed:       "40G",
    reachMeters: 150,
    reachLabel:  "SR4",
    fiberType:   "MMF",
    connector:   "MPO",
    wavelengths: "850nm",
    standard:    "40GBASE-SR4",
    notes:       "40GbE QSFP+ SR4; Meta FBOSS Wedge100 aggregation uplink; OM3/OM4 MPO-12 up to 150m",
  },
  {
    pid:         "META-QSFP-40G-LR4",
    formFactor:  "QSFP+",
    speedGbps:   40,
    speed:       "40G",
    reachMeters: 10000,
    reachLabel:  "LR4",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1270-1330nm",
    standard:    "40GBASE-LR4",
    notes:       "40GbE QSFP+ LR4; Meta FBOSS Wedge100 inter-fabric link; OS2 SMF up to 10km",
  },
  // ── 100G QSFP28 ──────────────────────────────────────────────────────────
  {
    pid:         "META-QSFP28-100G-SR4",
    formFactor:  "QSFP28",
    speedGbps:   100,
    speed:       "100G",
    reachMeters: 100,
    reachLabel:  "SR4",
    fiberType:   "MMF",
    connector:   "MPO",
    wavelengths: "850nm",
    standard:    "100GBASE-SR4",
    notes:       "100GbE QSFP28 SR4; Meta FBOSS Minipack fabric spine uplink; OM4 MPO-12 up to 100m",
  },
  {
    pid:         "META-QSFP28-100G-LR4",
    formFactor:  "QSFP28",
    speedGbps:   100,
    speed:       "100G",
    reachMeters: 10000,
    reachLabel:  "LR4",
    fiberType:   "SMF",
    connector:   "LC",
    wavelengths: "1295-1310nm",
    standard:    "100GBASE-LR4",
    notes:       "100GbE QSFP28 LR4; Meta FBOSS Minipack inter-pod DCI link; OS2 SMF up to 10km",
  },
  // ── 400G QSFP-DD ─────────────────────────────────────────────────────────
  {
    pid:         "META-QSFP-DD-400G-DR4",
    formFactor:  "QSFP-DD",
    speedGbps:   400,
    speed:       "400G",
    reachMeters: 500,
    reachLabel:  "DR4",
    fiberType:   "SMF",
    connector:   "MPO",
    wavelengths: "1310nm",
    standard:    "400GBASE-DR4",
    notes:       "400GbE QSFP-DD DR4; Meta Minipack2/Montara OCP 400G spine interconnect; OS2 SMF up to 500m",
  },
  {
    pid:         "META-QSFP-DD-400G-SR8",
    formFactor:  "QSFP-DD",
    speedGbps:   400,
    speed:       "400G",
    reachMeters: 100,
    reachLabel:  "SR8",
    fiberType:   "MMF",
    connector:   "MPO",
    wavelengths: "850nm",
    standard:    "400GBASE-SR8",
    notes:       "400GbE QSFP-DD SR8; Meta Minipack2/Montara OCP 400G fabric; OM4/OM5 MPO-16 up to 100m",
  },
  // ── 800G QSFP-DD (OCP FBOSS next-gen) ────────────────────────────────────
  {
    pid:         "META-QSFP-DD-800G-SR8",
    formFactor:  "QSFP-DD",
    speedGbps:   800,
    speed:       "800G",
    reachMeters: 100,
    reachLabel:  "SR8",
    fiberType:   "MMF",
    connector:   "MPO",
    wavelengths: "850nm",
    standard:    "800GBASE-SR8",
    notes:       "800GbE QSFP-DD SR8; Meta OCP FBOSS next-gen 800G fabric (Wedge400 upgrade cycle); OM5 MPO-16 up to 100m",
  },
  // ── 800G OSFP (OCP FBOSS next-gen) ───────────────────────────────────────
  {
    pid:         "META-OSFP-800G-DR8",
    formFactor:  "OSFP",
    speedGbps:   800,
    speed:       "800G",
    reachMeters: 500,
    reachLabel:  "DR8",
    fiberType:   "SMF",
    connector:   "MPO",
    wavelengths: "1310nm",
    standard:    "800GBASE-DR8",
    notes:       "800G OSFP DR8; Meta OCP 800G OSFP for Minipack3 datacenter spine; OS2 SMF MPO-16 up to 500m; OCP FBOSS platform",
  },
];

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

  const vendorId = await ensureVendor(
    "Meta",
    "oem",
    "https://engineering.fb.com/category/networking-traffic/",
    undefined
  );

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

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

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