CREATE TABLE profiles (id uuid PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE, display_name text NOT NULL DEFAULT '', created_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE boards (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), owner_id uuid NOT NULL UNIQUE REFERENCES profiles(id) ON DELETE CASCADE, slug text NOT NULL UNIQUE, name text NOT NULL, welcome_message text NOT NULL DEFAULT '', logo_path text, accent_color text NOT NULL DEFAULT '#2563eb', created_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE requests (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), board_id uuid NOT NULL REFERENCES boards(id) ON DELETE CASCADE, author_id uuid REFERENCES profiles(id) ON DELETE SET NULL, created_by_maker boolean NOT NULL DEFAULT false, title text NOT NULL CHECK (char_length(title) BETWEEN 3 AND 120), description text NOT NULL DEFAULT '' CHECK (char_length(description) <= 2000), status text NOT NULL DEFAULT 'open' CHECK (status IN ('open','planned','in_progress','shipped','declined')), vote_count integer NOT NULL DEFAULT 0 CHECK (vote_count >= 0), merged_into_id uuid REFERENCES requests(id), deleted_at timestamptz, shipped_at timestamptz, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), CHECK (merged_into_id IS NULL OR merged_into_id <> id));
CREATE TABLE request_intake (request_id uuid PRIMARY KEY REFERENCES requests(id) ON DELETE CASCADE, source_note text NOT NULL DEFAULT '', contact_email text, created_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE votes (request_id uuid NOT NULL REFERENCES requests(id) ON DELETE CASCADE, voter_id uuid NOT NULL REFERENCES profiles(id) ON DELETE CASCADE, moved_from_request_id uuid REFERENCES requests(id) ON DELETE SET NULL, created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (request_id, voter_id));
CREATE TABLE blocked_voters (board_id uuid NOT NULL REFERENCES boards(id) ON DELETE CASCADE, user_id uuid NOT NULL REFERENCES profiles(id) ON DELETE CASCADE, reason text NOT NULL DEFAULT '', created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (board_id, user_id));
CREATE TABLE changelog_entries (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), board_id uuid NOT NULL REFERENCES boards(id) ON DELETE CASCADE, title text NOT NULL, body text NOT NULL DEFAULT '', status text NOT NULL DEFAULT 'draft' CHECK (status IN ('draft','published')), entry_date date NOT NULL DEFAULT current_date, published_at timestamptz, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE changelog_entry_requests (entry_id uuid NOT NULL REFERENCES changelog_entries(id) ON DELETE CASCADE, request_id uuid NOT NULL REFERENCES requests(id) ON DELETE CASCADE, PRIMARY KEY (entry_id, request_id));
CREATE TABLE notification_outbox (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), board_id uuid NOT NULL REFERENCES boards(id) ON DELETE CASCADE, request_id uuid NOT NULL REFERENCES requests(id) ON DELETE CASCADE, recipient_user_id uuid REFERENCES profiles(id) ON DELETE SET NULL, recipient_email text NOT NULL, kind text NOT NULL DEFAULT 'shipped' CHECK (kind IN ('shipped')), status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending','sent','failed','skipped')), idempotency_key text NOT NULL UNIQUE, attempts integer NOT NULL DEFAULT 0, last_error text, provider_message_id text, created_at timestamptz NOT NULL DEFAULT now(), sent_at timestamptz, opened_at timestamptz);
CREATE TABLE email_suppressions (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), email text NOT NULL UNIQUE, reason text NOT NULL CHECK (reason IN ('unsubscribed','bounced','complaint')), created_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE moderation_log (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), board_id uuid NOT NULL REFERENCES boards(id) ON DELETE CASCADE, actor_id uuid NOT NULL REFERENCES profiles(id), action text NOT NULL CHECK (action IN ('status_change','merge','edit_request','delete_request','restore_request','block_voter','unblock_voter','publish_changelog')), request_id uuid REFERENCES requests(id) ON DELETE SET NULL, detail jsonb NOT NULL DEFAULT '{}'::jsonb, created_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE plan_entitlements (plan text NOT NULL CHECK (plan IN ('free','starter','pro')), entitlement_key text NOT NULL, int_value integer NOT NULL, PRIMARY KEY (plan, entitlement_key));
CREATE TABLE subscriptions (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), board_id uuid NOT NULL UNIQUE REFERENCES boards(id) ON DELETE CASCADE, plan text NOT NULL DEFAULT 'free' CHECK (plan IN ('free','starter','pro')), status text NOT NULL DEFAULT 'active' CHECK (status IN ('active','trialing','past_due','canceled')), provider text NOT NULL DEFAULT 'stripe', provider_customer_id text, provider_subscription_id text UNIQUE, current_period_end timestamptz, checkout_idempotency_key text UNIQUE, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE billing_events (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), provider text NOT NULL, provider_event_id text NOT NULL, event_type text NOT NULL, board_id uuid REFERENCES boards(id) ON DELETE SET NULL, payload jsonb NOT NULL, processed_at timestamptz, created_at timestamptz NOT NULL DEFAULT now(), UNIQUE (provider, provider_event_id));