-- t-paliad-313 (m/paliad#141): Composer Slice A — per-draft section rows.
--
-- paliad.submission_sections holds one row per (draft, section_key) for
-- Composer-mode drafts. Slice A seeds rows on draft create from the
-- base's section_spec.defaults; the editor renders them read-only. Slice
-- B turns them editable, Slice F adds reorder/hide/add-custom.
--
-- kind values per the design (Q10 ratification — no *_auto kind):
--   'prose'    — free Markdown content (default).
--   'requests' — Anträge-style content (editor may add auto-numbering
--                later; Slice A treats identical to 'prose').
--   'evidence' — Beweisangebote (editor may prefix lines with
--                'Beweis: '; Slice A treats identical to 'prose').
--
-- Visibility flows through draft_id → submission_drafts → can_see_project
-- + owner-scoped. RLS policies mirror the four-policy shape on
-- submission_drafts so seeding from the Go service stays inside the
-- same RLS envelope.
--
-- content_md_de + content_md_en both NOT NULL DEFAULT '' so neither
-- side blocks the bilingual-by-construction render path. Empty content
-- renders as the missing-content marker per the editor's contract.
--
-- Per the brief (head's instruction msg #2392) Slice A does NOT auto-
-- upgrade the 11 pre-Composer drafts — those remain base_id=NULL with
-- no section rows. The v1 fallback render path stays compiled in to
-- keep them working.

CREATE TABLE IF NOT EXISTS paliad.submission_sections (
    id            uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    draft_id      uuid NOT NULL REFERENCES paliad.submission_drafts(id) ON DELETE CASCADE,
    section_key   text NOT NULL,
    order_index   int  NOT NULL,
    kind          text NOT NULL,
    label_de      text NOT NULL,
    label_en      text NOT NULL,
    included      bool NOT NULL DEFAULT true,
    content_md_de text NOT NULL DEFAULT '',
    content_md_en text NOT NULL DEFAULT '',
    created_at    timestamptz NOT NULL DEFAULT now(),
    updated_at    timestamptz NOT NULL DEFAULT now(),

    CONSTRAINT submission_sections_kind_check
        CHECK (kind IN ('prose', 'requests', 'evidence')),
    CONSTRAINT submission_sections_unique_per_draft
        UNIQUE (draft_id, section_key)
);

CREATE INDEX IF NOT EXISTS submission_sections_draft_idx
    ON paliad.submission_sections (draft_id, order_index);

ALTER TABLE paliad.submission_sections ENABLE ROW LEVEL SECURITY;

DROP POLICY IF EXISTS submission_sections_select ON paliad.submission_sections;
CREATE POLICY submission_sections_select
    ON paliad.submission_sections FOR SELECT TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM paliad.submission_drafts d
             WHERE d.id = paliad.submission_sections.draft_id
               AND d.user_id = auth.uid()
               AND (d.project_id IS NULL OR paliad.can_see_project(d.project_id))
        )
    );

DROP POLICY IF EXISTS submission_sections_insert ON paliad.submission_sections;
CREATE POLICY submission_sections_insert
    ON paliad.submission_sections FOR INSERT TO authenticated
    WITH CHECK (
        EXISTS (
            SELECT 1 FROM paliad.submission_drafts d
             WHERE d.id = paliad.submission_sections.draft_id
               AND d.user_id = auth.uid()
               AND (d.project_id IS NULL OR paliad.can_see_project(d.project_id))
        )
    );

DROP POLICY IF EXISTS submission_sections_update ON paliad.submission_sections;
CREATE POLICY submission_sections_update
    ON paliad.submission_sections FOR UPDATE TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM paliad.submission_drafts d
             WHERE d.id = paliad.submission_sections.draft_id
               AND d.user_id = auth.uid()
               AND (d.project_id IS NULL OR paliad.can_see_project(d.project_id))
        )
    )
    WITH CHECK (
        EXISTS (
            SELECT 1 FROM paliad.submission_drafts d
             WHERE d.id = paliad.submission_sections.draft_id
               AND d.user_id = auth.uid()
               AND (d.project_id IS NULL OR paliad.can_see_project(d.project_id))
        )
    );

DROP POLICY IF EXISTS submission_sections_delete ON paliad.submission_sections;
CREATE POLICY submission_sections_delete
    ON paliad.submission_sections FOR DELETE TO authenticated
    USING (
        EXISTS (
            SELECT 1 FROM paliad.submission_drafts d
             WHERE d.id = paliad.submission_sections.draft_id
               AND d.user_id = auth.uid()
               AND (d.project_id IS NULL OR paliad.can_see_project(d.project_id))
        )
    );

DROP TRIGGER IF EXISTS submission_sections_set_updated_at ON paliad.submission_sections;
CREATE TRIGGER submission_sections_set_updated_at
    BEFORE UPDATE ON paliad.submission_sections
    FOR EACH ROW EXECUTE FUNCTION paliad.tg_set_updated_at();

COMMENT ON TABLE paliad.submission_sections IS
    't-paliad-313: per-draft Composer section rows. Slice A: seeded on draft create from base.section_spec.defaults, rendered read-only. Slice B: editable. RLS mirrors submission_drafts (owner-scoped + can_see_project).';
