-- sql/132 — Wavelength authority correction + format normalization
--
-- Follow-up to sql/131. Discovery: transceivers.wavelengths holds the part-derived
-- wavelength as a BARE number ("1470") for ~16.8k rows, which tip_extract_wavelength_nm()
-- cannot read (it requires the "nm" suffix). Meanwhile wavelength_tx_nm disagrees with
-- that authoritative bare number 33% of the time, and 64% of those disagreements are a
-- systematic tx_nm=1310 enricher default. So the bare-number column is authoritative;
-- tx_nm is not. This migration prefers the authoritative source.
--
--   1) normalize authoritative bare-number wavelengths -> "NNNnm" (format only, no value change)
--   2) clean "N/A" text -> NULL
--   3) correct the few CWDM rows sql/131 backfilled with a wrong tx_nm default, using the
--      deterministic CWDM wavelength in the part number
--   4) re-promote pending -> auto_approved where the FULL spec gate now agrees on
--      authoritative data (gate already protects against tx_nm noise: both sides must
--      agree within +/-10nm, so a wrong 1310 default can never auto-approve a real 1470).

BEGIN;

-- 1) normalize authoritative bare-number wavelengths (preserve value, add unit)
UPDATE transceivers
   SET wavelengths = wavelengths || 'nm'
 WHERE wavelengths ~ '^\d{3,4}$';

-- 2) non-values -> NULL (N/A, -, empty, etc.)
UPDATE transceivers
   SET wavelengths = NULL
 WHERE wavelengths IS NOT NULL
   AND tip_extract_wavelength_nm(wavelengths) IS NULL
   AND wavelengths !~ '\d';

-- 3) correct CWDM rows that sql/131 filled from a wrong tx_nm default:
--    use the deterministic CWDM wavelength encoded in the part number
UPDATE transceivers
   SET wavelengths = (regexp_match(part_number,'(12[6-9]0|13[0-9]0|14[0-9]0|15[0-9]0|16[01]0)'))[1] || 'nm'
 WHERE wavelengths = wavelength_tx_nm || 'nm'
   AND part_number ~ '(12[6-9]|13[0-9]|14[0-9]|15[0-9]|16[01])0'
   AND (regexp_match(part_number,'(12[6-9]0|13[0-9]0|14[0-9]0|15[0-9]0|16[01]0)'))[1]::int <> wavelength_tx_nm;

-- 3b) upgrade the sync trigger so future ingests self-normalize:
--     prefer an authoritative bare-number wavelength (add "nm"), fall back to tx_nm only
--     when no textual wavelength is present at all.
CREATE OR REPLACE FUNCTION tip_sync_wavelengths_from_tx() RETURNS trigger AS $fn$
BEGIN
  IF NEW.wavelengths ~ '^\d{3,4}$' THEN
    NEW.wavelengths := NEW.wavelengths || 'nm';
  ELSIF (NEW.wavelengths IS NULL OR NEW.wavelengths = '' OR tip_extract_wavelength_nm(NEW.wavelengths) IS NULL)
        AND NEW.wavelength_tx_nm IS NOT NULL THEN
    NEW.wavelengths := NEW.wavelength_tx_nm || 'nm';
  END IF;
  RETURN NEW;
END;
$fn$ LANGUAGE plpgsql;

-- 4) valid promotion on corrected/authoritative data (same full spec gate as spec-matcher)
UPDATE transceiver_equivalences e
   SET status      = 'auto_approved',
       confidence  = GREATEST(e.confidence, 0.85),
       match_notes = COALESCE(e.match_notes,'') || ' | sql/132: full spec confirmed on authoritative wavelength',
       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;
