-- t-paliad-157 / m's 2026-05-08 batch Item 2: mark rules that are not
-- always part of every proceeding instance as optional. Examples m
-- named: Antrag auf Kostenentscheidung (RoP.151) — only fires when a
-- party files for it; some appeal-related deadlines that depend on
-- specific facts.
--
-- Distinct from is_mandatory: a rule can be is_mandatory=true (the
-- deadline is statutorily fixed when it applies) AND is_optional=true
-- (whether the deadline applies at all is a per-case choice). Default
-- false on backfill so the existing always-applies semantic stays.
--
-- Frontend reads is_optional and pre-unchecks the row in the save-
-- to-project modal; user toggles to opt in. Timeline view stays
-- unchanged — the rule still renders so the user sees what could
-- apply.

ALTER TABLE paliad.deadline_rules
    ADD COLUMN IF NOT EXISTS is_optional boolean NOT NULL DEFAULT false;

COMMENT ON COLUMN paliad.deadline_rules.is_optional IS
    'When true, the deadline is conditional on an act the user takes '
    '(filing a cost-decision request, choosing to appeal, etc.) and '
    'should be opt-in when saving to a project. Distinct from '
    'is_mandatory, which is about statutory strictness once the rule '
    'applies. Backfill: false on every existing row except a small '
    'starter set m identified (RoP.151).';

-- Starter backfill: m's explicit example. More flips can land via
-- targeted UPDATEs as m reviews the rule library.
UPDATE paliad.deadline_rules
   SET is_optional = true
 WHERE rule_code = 'RoP.151'
   AND is_active = true;
