-- t-paliad-164 / m's 2026-05-08 21:42 dogfood feedback: when the user
-- selects an Akte in the Determinator (Slice 3c perspective chip),
-- the chip should already be locked to the firm's known side instead
-- of asking the user to re-pick something the project already knows.
--
-- Add a project-level our_side text column. NULL = unknown / not set
-- (default), so existing projects stay neutral and the Determinator
-- falls back to free-pick. The chip values mirror event_categories.
-- party so the Determinator can predefine the chip without mapping.
--
-- 'court' is allowed for completeness (paliad runs internal projects
-- where the firm represents the court / a tribunal-side stakeholder
-- — rare but real); the Determinator currently only acts on
-- claimant / defendant.
--
-- Idempotent so re-runs against a partially-applied state stay safe
-- (live tracker is at v71; paliad has been bitten by collisions
-- twice this week, see m/paliad#15 commits and dirac's mig 070).

ALTER TABLE paliad.projects
    ADD COLUMN IF NOT EXISTS our_side text;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint
         WHERE conname = 'projects_our_side_check'
           AND conrelid = 'paliad.projects'::regclass
    ) THEN
        ALTER TABLE paliad.projects
            ADD CONSTRAINT projects_our_side_check
            CHECK (our_side IS NULL
                OR our_side IN ('claimant', 'defendant', 'court', 'both'));
    END IF;
END $$;

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. NULL = unknown / not set; Determinator '
    'falls back to free-pick.';
