-- Migration 107: Close stale manual equivalence review debt.
--
-- Pending equivalence candidates are only actionable while the Flexoptix
-- product still has unresolved competitor research. Once the product-level
-- state is terminal (`matched`, `no_valid_match`, or `ambiguous`), leftover
-- pending candidates are stale review debt and must not ask for manual work.

UPDATE transceiver_equivalences eq
SET status = 'rejected',
    reviewed_by = 'verify:close-stale-review-queue',
    reviewed_at = NOW(),
    reject_reason = CASE
      WHEN fx.competitor_status = 'matched'
        THEN 'automated closure: Flexoptix product already has a resolved competitor match; stale alternate candidate'
      WHEN fx.competitor_status = 'ambiguous'
        THEN 'automated closure: Flexoptix product competitor state is resolved ambiguous; no safe deterministic 1:1 match'
      WHEN fx.competitor_status = 'no_valid_match'
        THEN 'automated closure: Flexoptix product has resolved no-valid-match state'
      ELSE 'automated closure: stale non-actionable equivalence candidate'
    END,
    re_research_due_at = NULL,
    re_researched_at = NOW(),
    updated_at = NOW()
FROM transceivers fx
WHERE eq.flexoptix_id = fx.id
  AND eq.status = 'pending'
  AND COALESCE(fx.competitor_status, 'needs_research') IN ('matched', 'no_valid_match', 'ambiguous');

COMMENT ON TABLE transceiver_equivalences IS
  'Candidate equivalences between Flexoptix and competitor transceivers. Pending rows are actionable only while product-level competitor research is unresolved; resolved product states close stale pending candidates automatically.';
