-- Migration 075: Obligation Refinement Fields
-- Supports Merge Pass (implementation-level dedup) and metadata enrichment.
--
-- New fields:
--   merged_into_id    — points to survivor obligation when merged
--   trigger_type      — event / periodic / continuous
--   is_implementation_specific — true if obligation references concrete tool/protocol

-- =============================================================================
-- 1. Add merge tracking
-- =============================================================================

ALTER TABLE obligation_candidates
    ADD COLUMN IF NOT EXISTS merged_into_id UUID
        REFERENCES obligation_candidates(id);

CREATE INDEX IF NOT EXISTS idx_oc_merged_into
    ON obligation_candidates(merged_into_id)
    WHERE merged_into_id IS NOT NULL;

-- Allow 'merged' as release_state
ALTER TABLE obligation_candidates
    DROP CONSTRAINT IF EXISTS obligation_candidates_release_state_check;

ALTER TABLE obligation_candidates
    ADD CONSTRAINT obligation_candidates_release_state_check
        CHECK (release_state IN ('extracted', 'validated', 'rejected', 'composed', 'merged'));

-- =============================================================================
-- 2. Add enrichment metadata
-- =============================================================================

ALTER TABLE obligation_candidates
    ADD COLUMN IF NOT EXISTS trigger_type VARCHAR(20) DEFAULT NULL
        CHECK (trigger_type IS NULL OR trigger_type IN ('event', 'periodic', 'continuous'));

ALTER TABLE obligation_candidates
    ADD COLUMN IF NOT EXISTS is_implementation_specific BOOLEAN DEFAULT FALSE;
