-- Down migration for 112_client_role_rework.
--
-- Restores the original 4-value CHECK ('claimant','defendant',
-- 'court','both', NULL) and backfills any rows that landed on a new
-- sub-role value (applicant / appellant / respondent / third_party /
-- other) to NULL so the schema is internally consistent after the
-- step-down.

BEGIN;

-- Backfill new sub-role values to NULL so the old CHECK doesn't reject.
UPDATE paliad.projects
   SET our_side = NULL
 WHERE our_side IN ('applicant', 'appellant', 'respondent', 'third_party', 'other');

ALTER TABLE paliad.projects
    DROP CONSTRAINT IF EXISTS projects_our_side_check;

ALTER TABLE paliad.projects
    ADD CONSTRAINT projects_our_side_check
    CHECK (our_side IS NULL
        OR our_side IN ('claimant', 'defendant', 'court', 'both'));

COMMENT ON COLUMN paliad.projects.our_side IS
    'Which side the firm represents on this project. Used by the '
    'Fristenrechner Determinator (Slice 3c) to predefine the '
    'perspective chip from the project context. Allowed: claimant, '
    'defendant, court, both.';

COMMIT;
