-- t-paliad-271 Wave 2 Tier-3 Slice A — duration_unit CHECK constraint with
-- 'working_days' added to the allowed set.
--
-- Per docs/research-deadlines-completeness-2026-05-25.md Tier 3 Primitive 1
-- (T3.1) — the calculator gains a business-day arithmetic path for UPC RoP
-- R.198 / R.213 (and downstream for any rule that needs the 31d-OR-20wd
-- combine-max pattern). The schema currently accepts free-text on
-- duration_unit (no CHECK), which is why 'working_days' rows already exist
-- in the DB but were silently dropped by the calculator. Adding the CHECK
-- pins the contract and prevents typos.
--
-- alt_duration_unit gets the same constraint (NULL-tolerant) so the alt
-- path stays in lockstep with the primary path.
--
-- Idempotent: DROP CONSTRAINT IF EXISTS before ADD. Existing data was
-- audited via `SELECT DISTINCT duration_unit FROM paliad.deadline_rules`
-- on 2026-05-25 (returned only days/weeks/months) plus the two live
-- alt-unit rows already at 'working_days' — both shapes pass.
--
-- audit_reason set_config is NOT needed for DDL (mig 079 trigger fires on
-- INSERT/UPDATE/DELETE on the rows, not on ALTER TABLE).

ALTER TABLE paliad.deadline_rules
    DROP CONSTRAINT IF EXISTS deadline_rules_duration_unit_check;

ALTER TABLE paliad.deadline_rules
    ADD CONSTRAINT deadline_rules_duration_unit_check
    CHECK (duration_unit IN ('days', 'weeks', 'months', 'working_days'));

ALTER TABLE paliad.deadline_rules
    DROP CONSTRAINT IF EXISTS deadline_rules_alt_duration_unit_check;

ALTER TABLE paliad.deadline_rules
    ADD CONSTRAINT deadline_rules_alt_duration_unit_check
    CHECK (alt_duration_unit IS NULL
        OR alt_duration_unit IN ('days', 'weeks', 'months', 'working_days'));
