-- Phase A: feedback tables re-namespaced into paliad.*.
-- Replaces docs/migrations/00{1,2,3}.sql which put them in public schema.
--
-- Old tables (public.patholo_link_suggestions, public.patholo_link_feedback,
-- public.checklisten_feedback, public.gerichte_feedback) continue to exist
-- and are still referenced by the current handlers via PostgREST.
--
-- Phase A creates the paliad versions fresh (no data copy) so the rest of
-- the platform can adopt them. A follow-on phase (P1 handler refactor) will:
--   (1) swap handlers from PostgREST to the direct DB connection,
--   (2) copy any meaningful rows from the public tables,
--   (3) drop the public tables and this duplication.
--
-- This is flagged in the Phase A completion report.

-- Link suggestions (user-submitted via "Nützlichen Link vorschlagen")
CREATE TABLE paliad.link_suggestions (
    id             uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    title          text NOT NULL,
    url            text NOT NULL,
    category       text NOT NULL,
    description    text NOT NULL DEFAULT '',
    suggested_by   text NOT NULL DEFAULT '',
    status         text NOT NULL DEFAULT 'pending'
                   CHECK (status IN ('pending', 'approved', 'rejected')),
    created_at     timestamptz NOT NULL DEFAULT now()
);

-- Link feedback (per-card feedback icon)
CREATE TABLE paliad.link_feedback (
    id             uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    link_id        text NOT NULL,
    feedback_type  text NOT NULL,
    message        text NOT NULL DEFAULT '',
    submitted_by   text NOT NULL DEFAULT '',
    created_at     timestamptz NOT NULL DEFAULT now()
);

-- Checklisten feedback
CREATE TABLE paliad.checklisten_feedback (
    id             uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    feedback_type  text NOT NULL,
    checklist      text NOT NULL DEFAULT '',
    message        text NOT NULL,
    submitted_by   text NOT NULL DEFAULT '',
    created_at     timestamptz NOT NULL DEFAULT now()
);

-- Gerichtsverzeichnis feedback
CREATE TABLE paliad.gerichte_feedback (
    id             uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    court_id       text NOT NULL DEFAULT '',
    feedback_type  text NOT NULL,
    message        text NOT NULL,
    submitted_by   text NOT NULL DEFAULT '',
    created_at     timestamptz NOT NULL DEFAULT now()
);

-- RLS: authenticated users can insert; no general read (admin-only, via service role).
ALTER TABLE paliad.link_suggestions     ENABLE ROW LEVEL SECURITY;
ALTER TABLE paliad.link_feedback        ENABLE ROW LEVEL SECURITY;
ALTER TABLE paliad.checklisten_feedback ENABLE ROW LEVEL SECURITY;
ALTER TABLE paliad.gerichte_feedback    ENABLE ROW LEVEL SECURITY;

CREATE POLICY link_suggestions_insert     ON paliad.link_suggestions     FOR INSERT TO authenticated WITH CHECK (true);
CREATE POLICY link_feedback_insert        ON paliad.link_feedback        FOR INSERT TO authenticated WITH CHECK (true);
CREATE POLICY checklisten_feedback_insert ON paliad.checklisten_feedback FOR INSERT TO authenticated WITH CHECK (true);
CREATE POLICY gerichte_feedback_insert    ON paliad.gerichte_feedback    FOR INSERT TO authenticated WITH CHECK (true);

-- Indexes: suggestion-count widget filters on status; others are append-mostly.
CREATE INDEX link_suggestions_status_idx ON paliad.link_suggestions (status);
