CREATE TABLE profiles (id uuid PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE, email text NOT NULL, timezone text NOT NULL DEFAULT 'UTC', created_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE alert_preferences (user_id uuid PRIMARY KEY REFERENCES profiles(id) ON DELETE CASCADE, email_enabled boolean NOT NULL DEFAULT true, sms_enabled boolean NOT NULL DEFAULT false, phone_e164 text, phone_verified_at timestamptz, quiet_hours_start time, quiet_hours_end time, updated_at timestamptz NOT NULL DEFAULT now(), CHECK (NOT sms_enabled OR phone_verified_at IS NOT NULL), CHECK ((quiet_hours_start IS NULL) = (quiet_hours_end IS NULL)));
CREATE TABLE phone_verifications (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), user_id uuid NOT NULL REFERENCES profiles(id) ON DELETE CASCADE, phone_e164 text NOT NULL, code_hash text NOT NULL, attempts integer NOT NULL DEFAULT 0, expires_at timestamptz NOT NULL, consumed_at timestamptz, created_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE csv_imports (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), user_id uuid NOT NULL REFERENCES profiles(id) ON DELETE CASCADE, filename text NOT NULL, column_mapping jsonb NOT NULL, status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending','applied','failed')), rows_total integer NOT NULL DEFAULT 0, rows_created integer NOT NULL DEFAULT 0, rows_updated integer NOT NULL DEFAULT 0, rows_failed integer NOT NULL DEFAULT 0, error_report jsonb, idempotency_key text NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), UNIQUE (user_id, idempotency_key));
CREATE TABLE products (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), user_id uuid NOT NULL REFERENCES profiles(id) ON DELETE CASCADE, sku text NOT NULL, name text NOT NULL, current_stock integer NOT NULL DEFAULT 0 CHECK (current_stock >= 0), reorder_point integer CHECK (reorder_point >= 0), alert_armed boolean NOT NULL DEFAULT true, arm_cycle integer NOT NULL DEFAULT 1, sales_rate_per_day numeric(10,2) CHECK (sales_rate_per_day >= 0), lead_time_days integer CHECK (lead_time_days >= 0), cover_days integer NOT NULL DEFAULT 14 CHECK (cover_days > 0), stock_updated_at timestamptz NOT NULL DEFAULT now(), created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), UNIQUE (user_id, sku));
CREATE TABLE stock_updates (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), user_id uuid NOT NULL REFERENCES profiles(id) ON DELETE CASCADE, product_id uuid NOT NULL REFERENCES products(id) ON DELETE CASCADE, previous_stock integer, new_stock integer NOT NULL, source text NOT NULL CHECK (source IN ('manual','inline_edit','csv')), import_id uuid REFERENCES csv_imports(id) ON DELETE SET NULL, created_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE alert_events (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), user_id uuid NOT NULL REFERENCES profiles(id) ON DELETE CASCADE, product_id uuid NOT NULL REFERENCES products(id) ON DELETE CASCADE, arm_cycle integer NOT NULL, stock_at_crossing integer NOT NULL, reorder_point_at_crossing integer NOT NULL, suggested_qty integer, import_id uuid REFERENCES csv_imports(id) ON DELETE SET NULL, status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending','sent','cancelled','failed')), created_at timestamptz NOT NULL DEFAULT now(), resolved_at timestamptz, UNIQUE (product_id, arm_cycle));
CREATE TABLE notification_deliveries (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), user_id uuid NOT NULL REFERENCES profiles(id) ON DELETE CASCADE, channel text NOT NULL CHECK (channel IN ('email','sms')), destination text NOT NULL, status text NOT NULL DEFAULT 'queued' CHECK (status IN ('queued','sending','sent','delivered','failed')), send_after timestamptz NOT NULL DEFAULT now(), attempts integer NOT NULL DEFAULT 0, provider text, provider_message_id text, last_error text, idempotency_key text NOT NULL UNIQUE, created_at timestamptz NOT NULL DEFAULT now(), sent_at timestamptz, delivered_at timestamptz);
CREATE TABLE delivery_alert_events (delivery_id uuid NOT NULL REFERENCES notification_deliveries(id) ON DELETE CASCADE, alert_event_id uuid NOT NULL REFERENCES alert_events(id) ON DELETE CASCADE, PRIMARY KEY (delivery_id, alert_event_id));
CREATE TABLE subscriptions (user_id uuid PRIMARY KEY REFERENCES profiles(id) ON DELETE CASCADE, provider text NOT NULL, provider_customer_id text, provider_subscription_id text, plan_code text NOT NULL, status text NOT NULL CHECK (status IN ('trialing','active','past_due','canceled')), trial_ends_at timestamptz, current_period_end timestamptz, updated_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE webhook_events (id uuid PRIMARY KEY DEFAULT gen_random_uuid(), provider text NOT NULL, provider_event_id text NOT NULL, event_type text NOT NULL, payload jsonb NOT NULL, received_at timestamptz NOT NULL DEFAULT now(), processed_at timestamptz, UNIQUE (provider, provider_event_id));