CREATE TABLE plans (plan_key text PRIMARY KEY, name text NOT NULL, price_cents integer NOT NULL CHECK (price_cents >= 0), currency text NOT NULL DEFAULT 'usd', trial_days integer NOT NULL DEFAULT 0 CHECK (trial_days >= 0), video_enabled boolean NOT NULL DEFAULT true, max_video_seconds integer NOT NULL CHECK (max_video_seconds > 0), storage_bytes_limit bigint NOT NULL CHECK (storage_bytes_limit > 0), max_programs integer CHECK (max_programs IS NULL OR max_programs >= 0), active boolean NOT NULL DEFAULT true, created_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE coaches (id uuid PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE, display_name text NOT NULL, logo_path text, accent_color text NOT NULL DEFAULT '#4F46E5' CHECK (accent_color ~ '^#[0-9A-Fa-f]{6}$'), intro_message text, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE subscriptions (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), coach_id uuid NOT NULL UNIQUE REFERENCES coaches(id) ON DELETE CASCADE, plan_key text NOT NULL REFERENCES plans(plan_key), provider text NOT NULL, provider_customer_id text, provider_subscription_id text, status text NOT NULL CHECK (status IN ('trialing','active','past_due','canceled','expired')), trial_ends_at timestamptz, current_period_end timestamptz, cancel_at_period_end boolean NOT NULL DEFAULT false, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), UNIQUE (provider, provider_subscription_id));
CREATE TABLE checkout_attempts (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), coach_id uuid NOT NULL REFERENCES coaches(id) ON DELETE CASCADE, plan_key text NOT NULL REFERENCES plans(plan_key), idempotency_key text NOT NULL UNIQUE, provider text NOT NULL, provider_session_id text, status text NOT NULL DEFAULT 'created' CHECK (status IN ('created','completed','expired')), created_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, coach_id uuid REFERENCES coaches(id) ON DELETE SET NULL, payload jsonb NOT NULL, received_at timestamptz NOT NULL DEFAULT now(), processed_at timestamptz, UNIQUE (provider, provider_event_id));
CREATE TABLE programs (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), coach_id uuid NOT NULL REFERENCES coaches(id) ON DELETE CASCADE, name text NOT NULL, intro_message text, archived_at timestamptz, created_at timestamptz NOT NULL DEFAULT now(), UNIQUE (coach_id, name), UNIQUE (id, coach_id));
CREATE TABLE clients (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), coach_id uuid NOT NULL REFERENCES coaches(id) ON DELETE CASCADE, program_id uuid, name text NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), UNIQUE (id, coach_id), FOREIGN KEY (program_id, coach_id) REFERENCES programs (id, coach_id));
CREATE TABLE request_links (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), client_id uuid NOT NULL UNIQUE, coach_id uuid NOT NULL, token_hash text NOT NULL UNIQUE, status text NOT NULL DEFAULT 'created' CHECK (status IN ('created','sent','opened','submitted','closed')), expires_at timestamptz, sent_at timestamptz, opened_at timestamptz, submitted_at timestamptz, rotated_at timestamptz, created_at timestamptz NOT NULL DEFAULT now(), FOREIGN KEY (client_id, coach_id) REFERENCES clients (id, coach_id) ON DELETE CASCADE);
CREATE TABLE media_uploads (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), coach_id uuid NOT NULL REFERENCES coaches(id) ON DELETE CASCADE, link_id uuid NOT NULL REFERENCES request_links(id) ON DELETE CASCADE, kind text NOT NULL CHECK (kind IN ('video','poster','photo')), storage_path text NOT NULL UNIQUE, mime_type text NOT NULL, declared_size_bytes bigint NOT NULL CHECK (declared_size_bytes > 0), size_bytes bigint, duration_seconds numeric(6,2), status text NOT NULL DEFAULT 'issued' CHECK (status IN ('issued','uploaded','attached','expired')), expires_at timestamptz NOT NULL, created_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE submissions (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), coach_id uuid NOT NULL REFERENCES coaches(id) ON DELETE CASCADE, link_id uuid NOT NULL REFERENCES request_links(id) ON DELETE CASCADE, client_id uuid NOT NULL REFERENCES clients(id) ON DELETE CASCADE, client_submission_key uuid NOT NULL, kind text NOT NULL CHECK (kind IN ('text','video')), text_body text, submitter_name text NOT NULL, submitter_role text, photo_upload_id uuid REFERENCES media_uploads(id), video_upload_id uuid REFERENCES media_uploads(id), poster_upload_id uuid REFERENCES media_uploads(id), publish_consent boolean NOT NULL CHECK (publish_consent = true), consent_text text NOT NULL, consent_at timestamptz NOT NULL, status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending','approved','rejected')), approved_at timestamptz, reviewed_at timestamptz, created_at timestamptz NOT NULL DEFAULT now(), UNIQUE (link_id, client_submission_key), CHECK ((kind = 'text' AND text_body IS NOT NULL AND video_upload_id IS NULL) OR (kind = 'video' AND video_upload_id IS NOT NULL)), CHECK ((status = 'approved') = (approved_at IS NOT NULL)));
CREATE TABLE walls (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), coach_id uuid NOT NULL REFERENCES coaches(id) ON DELETE CASCADE, program_id uuid, public_id text NOT NULL UNIQUE, name text NOT NULL, layout text NOT NULL DEFAULT 'grid' CHECK (layout IN ('grid','carousel','single')), first_embed_loaded_at timestamptz, created_at timestamptz NOT NULL DEFAULT now(), FOREIGN KEY (program_id, coach_id) REFERENCES programs (id, coach_id));
CREATE TABLE wall_items (wall_id uuid NOT NULL REFERENCES walls(id) ON DELETE CASCADE, submission_id uuid NOT NULL REFERENCES submissions(id) ON DELETE CASCADE, pinned boolean NOT NULL DEFAULT false, hidden boolean NOT NULL DEFAULT false, sort_order integer, created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (wall_id, submission_id));