-- Migration 115: Flexoptix Product Detail Columns
-- Adds columns to store full product detail data from the Flexoptix API
-- (specifications array, compatibility matrix, laser type, receiver type, etc.)
-- so we can build rich datasheets and deepen the TIP comparison data.

-- ── New columns ──────────────────────────────────────────────────────────────

-- Raw specs blob: full [{label, value}, ...] array from API (specifications=1)
-- Useful for datasheet generation and ad-hoc queries without re-fetching
ALTER TABLE transceivers
  ADD COLUMN IF NOT EXISTS fx_specifications JSONB;

-- Full compatibility list from API: [{sku, compatible_to_vendor, original_part_number}, ...]
-- More granular than vendor_compat (which has pattern-based matching)
ALTER TABLE transceivers
  ADD COLUMN IF NOT EXISTS fx_compatibilities JSONB;

-- Structured spec fields parsed from fx_specifications
ALTER TABLE transceivers
  ADD COLUMN IF NOT EXISTS compliance_code     TEXT;          -- "LX SGMII", "SR4 100GBASE", "LR4", etc.

ALTER TABLE transceivers
  ADD COLUMN IF NOT EXISTS laser_type          TEXT;          -- "FP", "DFB", "VCSEL", "EML", "CW-SiPh"

ALTER TABLE transceivers
  ADD COLUMN IF NOT EXISTS receiver_type       TEXT;          -- "PIN", "APD", "Coherent"

ALTER TABLE transceivers
  ADD COLUMN IF NOT EXISTS supported_protocols TEXT[];        -- ["1GigE", "Fast Ethernet", "10GBase-SR", ...]

ALTER TABLE transceivers
  ADD COLUMN IF NOT EXISTS extinction_ratio_db NUMERIC(6,2); -- dB

ALTER TABLE transceivers
  ADD COLUMN IF NOT EXISTS cdr_support         BOOLEAN;       -- false = "none", true = integrated CDR

ALTER TABLE transceivers
  ADD COLUMN IF NOT EXISTS inbuilt_fec         BOOLEAN;       -- false = "No", true = integrated FEC

-- Tracking: when the full per-SKU detail sync last completed for this product
ALTER TABLE transceivers
  ADD COLUMN IF NOT EXISTS detail_synced_at    TIMESTAMPTZ;

-- ── Indexes ──────────────────────────────────────────────────────────────────

-- GIN index for JSONB compatibility search (e.g. "which FX products are
-- compatible with Cisco Nexus 9000 where OPN starts with N9K-?")
CREATE INDEX IF NOT EXISTS idx_transceivers_fx_compatibilities
  ON transceivers USING GIN (fx_compatibilities)
  WHERE fx_compatibilities IS NOT NULL;

-- Index for detail sync queue (find unseen or stale products quickly)
-- NB: partial index with NOW() is not allowed (non-immutable); use plain index instead
CREATE INDEX IF NOT EXISTS idx_transceivers_detail_synced_at
  ON transceivers (detail_synced_at NULLS FIRST);

-- ── Statistics ───────────────────────────────────────────────────────────────
DO $$
DECLARE
  fx_cnt INTEGER;
BEGIN
  SELECT COUNT(*) INTO fx_cnt
  FROM transceivers t
  JOIN vendors v ON v.id = t.vendor_id
  WHERE UPPER(v.name) LIKE '%FLEXOPTIX%';

  RAISE NOTICE 'Migration 115 complete.';
  RAISE NOTICE '  Total FX products: %', fx_cnt;
  RAISE NOTICE '  New columns added: fx_specifications, fx_compatibilities,';
  RAISE NOTICE '    compliance_code, laser_type, receiver_type,';
  RAISE NOTICE '    supported_protocols, extinction_ratio_db,';
  RAISE NOTICE '    cdr_support, inbuilt_fec, detail_synced_at';
  RAISE NOTICE '  Run enrich:flexoptix-details to populate.';
END $$;
