-- sql/131 — Wavelength column sync (root-cause fix for equivalence review backlog)
--
-- Root cause: the enricher populates the structured integer column
-- transceivers.wavelength_tx_nm, but the equivalence spec-matcher reads the TEXT
-- column transceivers.wavelengths via tip_extract_wavelength_nm(). 27,908 rows had
-- wavelength_tx_nm set while wavelengths was NULL, so the matcher could not confirm
-- wavelength on either side and dumped ~3,155 genuine equivalences into manual review.
--
-- Fix (no fabrication — copies existing structured data into the format the matcher
-- expects, and keeps it in sync going forward):
--   A) one-off backfill of transceivers.wavelengths from wavelength_tx_nm
--   B) durable BEFORE-trigger so future ingests stay in sync
--   C) promote pending -> auto_approved ONLY where the FULL spec-matcher criteria now
--      agree (form_factor + speed + fiber_type + reach-tier + wavelength +/-10nm).
--      Everything with a genuine reach-tier / fiber / wavelength difference stays pending.

BEGIN;

-- A) one-off backfill (existing structured data -> text column the matcher reads)
UPDATE transceivers
   SET wavelengths = wavelength_tx_nm || 'nm'
 WHERE (wavelengths IS NULL OR wavelengths = '')
   AND wavelength_tx_nm IS NOT NULL;

-- B) durable sync trigger for future inserts/updates
CREATE OR REPLACE FUNCTION tip_sync_wavelengths_from_tx() RETURNS trigger AS $fn$
BEGIN
  IF (NEW.wavelengths IS NULL OR NEW.wavelengths = '')
     AND NEW.wavelength_tx_nm IS NOT NULL THEN
    NEW.wavelengths := NEW.wavelength_tx_nm || 'nm';
  END IF;
  RETURN NEW;
END;
$fn$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS trg_sync_wavelengths ON transceivers;
CREATE TRIGGER trg_sync_wavelengths
  BEFORE INSERT OR UPDATE ON transceivers
  FOR EACH ROW EXECUTE FUNCTION tip_sync_wavelengths_from_tx();

-- C) valid promotion: pending -> auto_approved where full spec now confirmed
UPDATE transceiver_equivalences e
   SET status      = 'auto_approved',
       confidence  = GREATEST(e.confidence, 0.85),
       match_notes = COALESCE(e.match_notes,'') || ' | sql/131: full spec confirmed after wavelength column sync',
       updated_at  = now()
  FROM transceivers fx, transceivers cx
 WHERE e.flexoptix_id = fx.id
   AND e.competitor_id = cx.id
   AND e.status = 'pending'
   AND fx.form_factor = cx.form_factor
   AND fx.speed_gbps  = cx.speed_gbps
   AND fx.fiber_type IS NOT DISTINCT FROM cx.fiber_type
   AND fx.reach_meters >= 10 AND cx.reach_meters >= 10
   AND tip_reach_tier(fx.reach_meters) = tip_reach_tier(cx.reach_meters)
   AND tip_extract_wavelength_nm(fx.wavelengths) IS NOT NULL
   AND tip_extract_wavelength_nm(cx.wavelengths) IS NOT NULL
   AND abs(tip_extract_wavelength_nm(fx.wavelengths) - tip_extract_wavelength_nm(cx.wavelengths)) <= 10;

COMMIT;
