-- t-paliad-138: rollback dual-control approvals.
--
-- Reverses 054_approvals.up.sql:
--   1. Drop appointment + deadline approval columns.
--   2. Drop paliad.approval_requests.
--   3. Drop paliad.approval_policies.
--   4. Drop paliad.approval_role_level().
--   5. Restore project_teams.role CHECK without 'senior_pa'.
--
-- Step 5 will fail loudly if any user has been re-roled to 'senior_pa' —
-- intentional, mirrors the t-paliad-051 down strategy. Operator must
-- migrate those rows to another role before rolling back.

ALTER TABLE paliad.appointments
    DROP COLUMN IF EXISTS completed_at,
    DROP COLUMN IF EXISTS approved_at,
    DROP COLUMN IF EXISTS approved_by,
    DROP COLUMN IF EXISTS pending_request_id,
    DROP COLUMN IF EXISTS approval_status;

ALTER TABLE paliad.deadlines
    DROP COLUMN IF EXISTS approved_at,
    DROP COLUMN IF EXISTS approved_by,
    DROP COLUMN IF EXISTS pending_request_id,
    DROP COLUMN IF EXISTS approval_status;

DROP INDEX IF EXISTS paliad.deadlines_approval_status_pending_idx;
DROP INDEX IF EXISTS paliad.appointments_approval_status_pending_idx;

DROP TABLE IF EXISTS paliad.approval_requests;
DROP TABLE IF EXISTS paliad.approval_policies;

DROP FUNCTION IF EXISTS paliad.approval_role_level(text);

-- Drop by both English and the German-legacy name (see up migration §1).
ALTER TABLE paliad.project_teams DROP CONSTRAINT IF EXISTS projekt_teams_role_check;
ALTER TABLE paliad.project_teams DROP CONSTRAINT IF EXISTS project_teams_role_check;
ALTER TABLE paliad.project_teams ADD CONSTRAINT project_teams_role_check
    CHECK (role IN (
        'lead', 'associate', 'pa', 'of_counsel',
        'local_counsel', 'expert', 'observer'
    ));
