-- t-paliad-277 / m/paliad#109: per-draft party selection + import provenance.
--
-- Adds two columns to paliad.submission_drafts:
--
--   selected_parties uuid[] — IDs of paliad.parties rows the lawyer
--     has chosen to mention in this specific submission. An empty
--     array (the default) means "include every party on the project"
--     so all existing drafts keep their current rendering. Non-empty
--     restricts the variable bag to the chosen subset, grouped by
--     role in SubmissionVarsService.
--
--   last_imported_at timestamptz — when the lawyer last clicked
--     "Aus Projekt importieren" on the draft editor (or NULL if they
--     never did). The frontend surfaces this timestamp next to the
--     button so a stale draft is obvious at a glance.
--
-- Both columns are purely additive and nullable / default-bearing —
-- the migration is safe to apply with active drafts in the table.
-- No FK on selected_parties: paliad.parties is project-scoped and we
-- prune stale references on read inside SubmissionVarsService rather
-- than chasing FK cascades across two tables (the variable bag silently
-- drops any uuid that no longer matches a row in paliad.parties).

ALTER TABLE paliad.submission_drafts
    ADD COLUMN IF NOT EXISTS selected_parties uuid[] NOT NULL DEFAULT '{}'::uuid[],
    ADD COLUMN IF NOT EXISTS last_imported_at timestamptz;

COMMENT ON COLUMN paliad.submission_drafts.selected_parties IS
    't-paliad-277: party IDs (paliad.parties) the lawyer has chosen to mention in this submission. Empty array = include every party on the project (backward-compat default). Non-empty = restrict to subset, grouped by role.';

COMMENT ON COLUMN paliad.submission_drafts.last_imported_at IS
    't-paliad-277: timestamp of the last "Aus Projekt importieren" click — surfaced next to the button so the lawyer can see staleness at a glance. NULL = never imported.';
