CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA public;
--> statement-breakpoint
COMMENT ON EXTENSION pgcrypto IS 'cryptographic functions';
--> statement-breakpoint
CREATE EXTENSION IF NOT EXISTS vector WITH SCHEMA public;
--> statement-breakpoint
COMMENT ON EXTENSION vector IS 'vector data type and ivfflat and hnsw access methods';
--> statement-breakpoint
CREATE TYPE public.account_state_user AS ENUM (
    'ACTIVE',
    'FROZEN',
    'DELETED_PENDING',
    'DELETED'
);
--> statement-breakpoint
CREATE TYPE public.account_state_vendor AS ENUM (
    'PENDING_FIRST_APPROVAL',
    'PENDING_PROCESSOR',
    'ACTIVE',
    'VETERAN',
    'FROZEN',
    'BANNED',
    'REJECTED'
);
--> statement-breakpoint
CREATE TYPE public.admin_action AS ENUM (
    'APPROVE',
    'REJECT',
    'FREEZE',
    'UNFREEZE',
    'BAN',
    'REMOVE_REVIEW',
    'RESTORE_REVIEW',
    'ADD_QUANTITY',
    'GRANT_ADMIN',
    'REVOKE_ADMIN',
    'REQUEST_REFUND',
    'RESEND_RECEIPT',
    'KB_CREATE',
    'KB_UPDATE',
    'KB_DELETE',
    'SETTING_UPDATE',
    'OUTBOX_RETRY',
    'MARK_PAID',
    'MARK_PENDING'
);
--> statement-breakpoint
CREATE TYPE public.admin_target_type AS ENUM (
    'DEAL',
    'VENDOR',
    'REVIEW',
    'USER',
    'PURCHASE',
    'KB_ARTICLE',
    'SETTING',
    'OUTBOX_EVENT',
    'IMAGE',
    'SETTLEMENT'
);
--> statement-breakpoint
CREATE TYPE public.affiliate_enrollment_status AS ENUM (
    'active',
    'suspended',
    'revoked'
);
--> statement-breakpoint
CREATE TYPE public.affiliate_entry_type AS ENUM (
    'affiliate_commission',
    'referral_reward',
    'refund_clawback',
    'adjustment',
    'redemption'
);
--> statement-breakpoint
CREATE TYPE public.affiliate_payout_status AS ENUM (
    'requested',
    'approved',
    'processing',
    'paid',
    'failed',
    'cancelled'
);
--> statement-breakpoint
CREATE TYPE public.affiliate_stripe_status AS ENUM (
    'none',
    'pending',
    'restricted',
    'enabled',
    'rejected'
);
--> statement-breakpoint
CREATE TYPE public.ai_decision AS ENUM (
    'PASS',
    'FLAG',
    'REJECT',
    'ERROR'
);
--> statement-breakpoint
CREATE TYPE public.ai_intervention_decision AS ENUM (
    'auto',
    'escalate',
    'ask',
    'propose'
);
--> statement-breakpoint
CREATE TYPE public.appeal_status AS ENUM (
    'PENDING',
    'APPROVED',
    'REJECTED'
);
--> statement-breakpoint
CREATE TYPE public.approved_by AS ENUM (
    'HUMAN',
    'AI_AGENT'
);
--> statement-breakpoint
CREATE TYPE public.avatar_type AS ENUM (
    'ICON',
    'GRAVATAR',
    'UPLOADED'
);
--> statement-breakpoint
CREATE TYPE public.cancellation_policy AS ENUM (
    'LEGAL_ONLY',
    'FLEXIBLE',
    'CUSTOM_WINDOW'
);
--> statement-breakpoint
CREATE TYPE public.carrier AS ENUM (
    'israel_post',
    'yedioth_couriers',
    'cheetah',
    'hfd',
    'baldar',
    'pickup',
    'other',
    'wolt_drive'
);
--> statement-breakpoint
CREATE TYPE public.case_offer_by AS ENUM (
    'vendor',
    'ai',
    'human'
);
--> statement-breakpoint
CREATE TYPE public.case_offer_outcome AS ENUM (
    'refund_full',
    'refund_partial',
    'replacement',
    'deny'
);
--> statement-breakpoint
CREATE TYPE public.case_offer_status AS ENUM (
    'pending',
    'accepted',
    'rejected',
    'superseded',
    'auto_executed'
);
--> statement-breakpoint
CREATE TYPE public.case_resolution_executor AS ENUM (
    'vendor_auto',
    'customer_accept',
    'ai_auto',
    'human'
);
--> statement-breakpoint
CREATE TYPE public.credit_entry_type AS ENUM (
    'referral_reward',
    'affiliate_commission',
    'redemption',
    'refund_clawback',
    'adjustment',
    'return_shipping_reimbursement'
);
--> statement-breakpoint
CREATE TYPE public.deal_state AS ENUM (
    'DRAFT',
    'UNDER_REVIEW',
    'PENDING_APPROVAL',
    'ACTIVE',
    'PAUSED',
    'SOLD_OUT',
    'EXPIRED',
    'REJECTED',
    'ARCHIVED'
);
--> statement-breakpoint
CREATE TYPE public.deal_type AS ENUM (
    'COUPON',
    'GROUP',
    'ITEM'
);
--> statement-breakpoint
CREATE TYPE public.email_verification_purpose AS ENUM (
    'signup',
    'change'
);
--> statement-breakpoint
CREATE TYPE public.fill_rule AS ENUM (
    'ALL_OR_NOTHING',
    'MINIMUM_THRESHOLD'
);
--> statement-breakpoint
CREATE TYPE public.group_deal_state AS ENUM (
    'COLLECTING',
    'THRESHOLD_MET',
    'EXECUTING',
    'PARTIAL_PENDING',
    'SUCCEEDED',
    'FAILED',
    'VENDOR_HONORED',
    'EXTENDED'
);
--> statement-breakpoint
CREATE TYPE public.image_approval_status AS ENUM (
    'PENDING',
    'APPROVED',
    'REJECTED'
);
--> statement-breakpoint
CREATE TYPE public.image_entity_type AS ENUM (
    'vendor',
    'deal',
    'user',
    'page_module',
    'support_ticket'
);
--> statement-breakpoint
CREATE TYPE public.invoice_provider AS ENUM (
    'morning',
    'icount',
    'rivhit',
    'invoice4u',
    'easycount',
    'meshulam',
    'self_handled'
);
--> statement-breakpoint
CREATE TYPE public.kyc_status AS ENUM (
    'none',
    'pending',
    'verified',
    'rejected'
);
--> statement-breakpoint
CREATE TYPE public.llm_decision AS ENUM (
    'APPROVE',
    'FLAG',
    'REJECT'
);
--> statement-breakpoint
CREATE TYPE public.llm_job_status AS ENUM (
    'PENDING',
    'RUNNING',
    'COMPLETED',
    'FAILED',
    'TIMED_OUT'
);
--> statement-breakpoint
CREATE TYPE public.llm_job_type AS ENUM (
    'DEAL_MODERATION',
    'IMAGE_APPROVAL',
    'REVIEW_PRESCORING',
    'VENDOR_VIOLATION',
    'TRANSLATION'
);
--> statement-breakpoint
CREATE TYPE public.llm_provider_type AS ENUM (
    'google',
    'openai-compat',
    'mock'
);
--> statement-breakpoint
CREATE TYPE public.llm_target_type AS ENUM (
    'DEAL',
    'IMAGE',
    'REVIEW_REMOVAL',
    'VENDOR',
    'CATEGORY',
    'TAG',
    'REVIEW'
);
--> statement-breakpoint
CREATE TYPE public.notification_severity AS ENUM (
    'info',
    'warning',
    'error'
);
--> statement-breakpoint
CREATE TYPE public.payment_brand AS ENUM (
    'VISA',
    'MASTERCARD',
    'AMEX',
    'BIT',
    'PAYPAL',
    'GOOGLE_PAY',
    'OTHER'
);
--> statement-breakpoint
CREATE TYPE public.payment_status AS ENUM (
    'PENDING',
    'COMPLETED',
    'REFUNDED',
    'PARTIAL_REFUND',
    'REFUND_REQUESTED',
    'FAILED'
);
--> statement-breakpoint
CREATE TYPE public.personal_deal_status AS ENUM (
    'PENDING',
    'ACCEPTED',
    'REJECTED',
    'EXPIRED'
);
--> statement-breakpoint
CREATE TYPE public.redemption_status AS ENUM (
    'UNREDEEMED',
    'REDEEMED',
    'EXPIRED',
    'CANCELLED'
);
--> statement-breakpoint
CREATE TYPE public.referral_kind AS ENUM (
    'referral',
    'affiliate'
);
--> statement-breakpoint
CREATE TYPE public.referral_status AS ENUM (
    'pending',
    'qualified',
    'rejected',
    'quarantined'
);
--> statement-breakpoint
CREATE TYPE public.removal_status AS ENUM (
    'NONE',
    'PENDING',
    'APPROVED',
    'REJECTED'
);
--> statement-breakpoint
CREATE TYPE public.removed_by AS ENUM (
    'HUMAN',
    'AI_AGENT'
);
--> statement-breakpoint
CREATE TYPE public.report_reason AS ENUM (
    'FACTUAL_ERROR',
    'MISLEADING',
    'WRONG_HOURS',
    'BUG',
    'OTHER'
);
--> statement-breakpoint
CREATE TYPE public.report_status AS ENUM (
    'OPEN',
    'INVESTIGATING',
    'RESOLVED',
    'DISMISSED'
);
--> statement-breakpoint
CREATE TYPE public.report_target_type AS ENUM (
    'VENDOR',
    'DEAL',
    'REVIEW',
    'USER'
);
--> statement-breakpoint
CREATE TYPE public.reservation_status AS ENUM (
    'HELD',
    'CAPTURED',
    'RELEASED',
    'CANCELLED',
    'WAITLISTED',
    'HOLD_EXPIRED'
);
--> statement-breakpoint
CREATE TYPE public.return_liability AS ENUM (
    'vendor',
    'buyer'
);
--> statement-breakpoint
CREATE TYPE public.return_reason AS ENUM (
    'defective',
    'damaged_in_transit',
    'not_as_described',
    'wrong_item_received',
    'changed_mind',
    'wrong_size',
    'arrived_late',
    'other'
);
--> statement-breakpoint
CREATE TYPE public.return_status AS ENUM (
    'requested',
    'approved',
    'in_transit',
    'received',
    'refunded',
    'denied',
    'cancelled',
    'expired'
);
--> statement-breakpoint
CREATE TYPE public.review_removal_queue_status AS ENUM (
    'PENDING',
    'APPROVED',
    'REJECTED'
);
--> statement-breakpoint
CREATE TYPE public.review_type AS ENUM (
    'STANDARD',
    'TECHNICAL'
);
--> statement-breakpoint
CREATE TYPE public.scan_status AS ENUM (
    'PENDING',
    'CLEAN',
    'INFECTED',
    'ERROR'
);
--> statement-breakpoint
CREATE TYPE public.sender_type AS ENUM (
    'USER',
    'VENDOR'
);
--> statement-breakpoint
CREATE TYPE public.share_channel AS ENUM (
    'whatsapp',
    'instagram',
    'facebook',
    'twitter',
    'telegram',
    'copy',
    'email',
    'sms',
    'other',
    'direct'
);
--> statement-breakpoint
CREATE TYPE public.shipment_status AS ENUM (
    'pending',
    'awaiting_ship',
    'shipped',
    'in_transit',
    'out_for_delivery',
    'delivered',
    'pickup_ready',
    'picked_up',
    'confirmed',
    'failed_delivery',
    'lost',
    'cancelled'
);
--> statement-breakpoint
CREATE TYPE public.shipping_mode AS ENUM (
    'flat',
    'free_over_threshold',
    'pickup_only',
    'free'
);
--> statement-breakpoint
CREATE TYPE public.support_attachment_abuse_status AS ENUM (
    'pending',
    'clean',
    'flagged',
    'rejected'
);
--> statement-breakpoint
CREATE TYPE public.support_case_category AS ENUM (
    'item_not_as_described',
    'no_show_vendor',
    'quality_issue',
    'cannot_redeem',
    'cancellation_request',
    'fraud',
    'safety',
    'other'
);
--> statement-breakpoint
CREATE TYPE public.support_case_status AS ENUM (
    'opened',
    'vendor_review',
    'vendor_offered',
    'escalated',
    'ai_handling',
    'awaiting_info',
    'human_review',
    'resolved',
    'closed',
    'reopened'
);
--> statement-breakpoint
CREATE TYPE public.support_kb_category AS ENUM (
    'faq',
    'policy',
    'refund_rules',
    'redemption_rules',
    'internal'
);
--> statement-breakpoint
CREATE TYPE public.support_kb_visibility AS ENUM (
    'public',
    'internal'
);
--> statement-breakpoint
CREATE TYPE public.support_message_author_type AS ENUM (
    'customer',
    'vendor',
    'ai',
    'human_agent',
    'system',
    'admin'
);
--> statement-breakpoint
CREATE TYPE public.support_message_visibility AS ENUM (
    'public',
    'vendor_internal',
    'site_internal'
);
--> statement-breakpoint
CREATE TYPE public.support_opener_role AS ENUM (
    'customer',
    'vendor'
);
--> statement-breakpoint
CREATE TYPE public.support_parent_type AS ENUM (
    'ticket',
    'case',
    'return',
    'pending_return'
);
--> statement-breakpoint
CREATE TYPE public.support_priority AS ENUM (
    'low',
    'normal',
    'high',
    'urgent'
);
--> statement-breakpoint
CREATE TYPE public.support_ticket_category AS ENUM (
    'account',
    'billing',
    'bug',
    'how_to',
    'complaint',
    'other'
);
--> statement-breakpoint
CREATE TYPE public.support_ticket_status AS ENUM (
    'open',
    'ai_handling',
    'awaiting_user',
    'escalated',
    'awaiting_agent',
    'human_handling',
    'resolved',
    'closed',
    'reopened'
);
--> statement-breakpoint
CREATE TYPE public.upload_purpose AS ENUM (
    'deal_image',
    'vendor_logo',
    'vendor_hero',
    'vendor_gallery',
    'user_gallery',
    'avatar',
    'homepage_banner',
    'support_attachment'
);
--> statement-breakpoint
CREATE TYPE public.variant_axis_kind AS ENUM (
    'SIZE',
    'COLOR',
    'TIMESLOT',
    'CUSTOM'
);
--> statement-breakpoint
CREATE TYPE public.vendor_payout_release_status AS ENUM (
    'held',
    'enqueued',
    'releasing',
    'released',
    'refunded',
    'cancelled'
);
--> statement-breakpoint
CREATE TYPE public.vendor_tier AS ENUM (
    'NEW',
    'VETERAN'
);
--> statement-breakpoint
CREATE FUNCTION public.assert_deal_complete_for_active() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
BEGIN
  IF NEW.deal_state = 'ACTIVE'
     AND (TG_OP = 'INSERT' OR OLD.deal_state IS DISTINCT FROM 'ACTIVE') THEN
    IF NEW.category_id IS NULL THEN
      RAISE EXCEPTION 'deal_incomplete_for_active: category_id is required'
        USING ERRCODE = 'check_violation';
    END IF;
    IF NEW.description IS NULL OR btrim(NEW.description) = '' THEN
      RAISE EXCEPTION 'deal_incomplete_for_active: description is required'
        USING ERRCODE = 'check_violation';
    END IF;
    IF NOT EXISTS (SELECT 1 FROM deal_images WHERE deal_id = NEW.id) THEN
      RAISE EXCEPTION 'deal_incomplete_for_active: at least one image is required'
        USING ERRCODE = 'check_violation';
    END IF;
  END IF;
  RETURN NEW;
END;
$$;
--> statement-breakpoint
CREATE FUNCTION public.safe_pgp_sym_decrypt(data bytea, key text) RETURNS text
    LANGUAGE plpgsql IMMUTABLE STRICT
    AS $$
BEGIN
  RETURN pgp_sym_decrypt(data, key);
EXCEPTION WHEN OTHERS THEN
  RETURN NULL;
END;
$$;
--> statement-breakpoint
CREATE FUNCTION public.show_db_tree() RETURNS TABLE(tree_structure text)
    LANGUAGE plpgsql
    AS $$
BEGIN
    -- First show all databases
    RETURN QUERY
    SELECT ':file_folder: ' || datname || ' (DATABASE)'
    FROM pg_database
    WHERE datistemplate = false;
    -- Then show current database structure
    RETURN QUERY
    WITH RECURSIVE
    -- Get schemas
    schemas AS (
        SELECT
            n.nspname AS object_name,
            1 AS level,
            n.nspname AS path,
            'SCHEMA' AS object_type
        FROM pg_namespace n
        WHERE n.nspname NOT LIKE 'pg_%'
        AND n.nspname != 'information_schema'
    ),
    -- Get all objects (tables, views, functions, etc.)
    objects AS (
        SELECT
            c.relname AS object_name,
            2 AS level,
            s.path || ' → ' || c.relname AS path,
            CASE c.relkind
                WHEN 'r' THEN 'TABLE'
                WHEN 'v' THEN 'VIEW'
                WHEN 'm' THEN 'MATERIALIZED VIEW'
                WHEN 'i' THEN 'INDEX'
                WHEN 'S' THEN 'SEQUENCE'
                WHEN 'f' THEN 'FOREIGN TABLE'
            END AS object_type
        FROM pg_class c
        JOIN pg_namespace n ON n.oid = c.relnamespace
        JOIN schemas s ON n.nspname = s.object_name
        WHERE c.relkind IN ('r','v','m','i','S','f')
        UNION ALL
        SELECT
            p.proname AS object_name,
            2 AS level,
            s.path || ' → ' || p.proname AS path,
            'FUNCTION' AS object_type
        FROM pg_proc p
        JOIN pg_namespace n ON n.oid = p.pronamespace
        JOIN schemas s ON n.nspname = s.object_name
    ),
    -- Combine schemas and objects
    combined AS (
        SELECT * FROM schemas
        UNION ALL
        SELECT * FROM objects
    )
    -- Final output with tree-like formatting
    SELECT
        REPEAT('    ', level) ||
        CASE
            WHEN level = 1 THEN '└── :open_file_folder: '
            ELSE '    └── ' ||
                CASE object_type
                    WHEN 'TABLE' THEN ':bar_chart: '
                    WHEN 'VIEW' THEN ':eye: '
                    WHEN 'MATERIALIZED VIEW' THEN ':newspaper: '
                    WHEN 'FUNCTION' THEN ':zap: '
                    WHEN 'INDEX' THEN ':mag: '
                    WHEN 'SEQUENCE' THEN ':1234: '
                    WHEN 'FOREIGN TABLE' THEN ':globe_with_meridians: '
                    ELSE ''
                END
        END || object_name || ' (' || object_type || ')'
    FROM combined
    ORDER BY path;
END;
$$;
--> statement-breakpoint
CREATE TABLE public.access_grant (
    id uuid NOT NULL,
    order_id uuid NOT NULL,
    item_id text NOT NULL,
    owner_key text NOT NULL,
    blob_key text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.address_zip_cache_misses (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    city_code text NOT NULL,
    street_code text,
    house_number text,
    resolution text NOT NULL,
    resolved_zip text,
    user_id uuid,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.addresses (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    label text NOT NULL,
    full_address text NOT NULL,
    lat numeric(10,7) NOT NULL,
    lng numeric(10,7) NOT NULL,
    is_default boolean DEFAULT false NOT NULL,
    is_gps boolean DEFAULT false NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.admin_actions (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    admin_id uuid,
    target_type public.admin_target_type NOT NULL,
    target_id uuid NOT NULL,
    action public.admin_action NOT NULL,
    note text,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.admin_assignments (
    scope text NOT NULL,
    entity_id uuid NOT NULL,
    admin_user_id uuid NOT NULL,
    assigned_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.affiliate_admin_actions (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    ts timestamp with time zone DEFAULT now() NOT NULL,
    admin_user_id uuid,
    target_user_id uuid,
    action text NOT NULL,
    payload jsonb NOT NULL,
    reason text
);
--> statement-breakpoint
CREATE TABLE public.affiliate_enrollments (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    status public.affiliate_enrollment_status NOT NULL,
    commission_pct integer,
    enrolled_at timestamp with time zone DEFAULT now() NOT NULL,
    suspended_at timestamp with time zone,
    suspended_reason text,
    stripe_account_id text,
    stripe_status public.affiliate_stripe_status DEFAULT 'none'::public.affiliate_stripe_status NOT NULL,
    stripe_payouts_enabled boolean DEFAULT false NOT NULL,
    stripe_updated_at timestamp with time zone,
    tos_accepted_at timestamp with time zone NOT NULL,
    tos_version text NOT NULL,
    notes text
);
--> statement-breakpoint
CREATE TABLE public.affiliate_entries (
    entry_id uuid NOT NULL,
    owner_id text NOT NULL,
    entry_type public.affiliate_entry_type NOT NULL,
    source_type text NOT NULL,
    source_id text NOT NULL,
    referral_id uuid,
    resolved_pct smallint,
    memo text,
    consumed_at timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE public.affiliate_payouts (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    enrollment_id uuid NOT NULL,
    amount_agorot bigint NOT NULL,
    status public.affiliate_payout_status NOT NULL,
    requested_at timestamp with time zone DEFAULT now() NOT NULL,
    approved_at timestamp with time zone,
    approved_by_user_id uuid,
    processing_at timestamp with time zone,
    paid_at timestamp with time zone,
    failed_at timestamp with time zone,
    failure_reason text,
    stripe_transfer_id text,
    stripe_payout_id text,
    idempotency_key text NOT NULL,
    ledger_entry_id uuid,
    settlement_rail text,
    settlement_reference text,
    settled_at timestamp with time zone,
    settled_by_user_id uuid,
    CONSTRAINT affiliate_payouts_amount_agorot_check CHECK ((amount_agorot > 0))
);
--> statement-breakpoint
CREATE TABLE public.agent_definition_versions (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    definition_id uuid NOT NULL,
    version integer NOT NULL,
    snapshot jsonb NOT NULL,
    changed_by uuid,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.agent_definitions (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    slug text NOT NULL,
    name text NOT NULL,
    description text DEFAULT ''::text NOT NULL,
    system_prompt_template text NOT NULL,
    knowledge_adapter_ids text[] DEFAULT '{}'::text[] NOT NULL,
    escalation_rule_ids text[] DEFAULT '{}'::text[] NOT NULL,
    available_decisions text[] DEFAULT '{}'::text[] NOT NULL,
    llm_chain jsonb NOT NULL,
    sla_ai_hours integer DEFAULT 4 NOT NULL,
    sla_human_hours integer DEFAULT 24 NOT NULL,
    max_cost_cents integer DEFAULT 10 NOT NULL,
    max_tool_calls integer DEFAULT 12 NOT NULL,
    ai_confidence_threshold numeric(4,3) DEFAULT 0.700 NOT NULL,
    auto_refund_cap_cents integer DEFAULT 0 NOT NULL,
    is_active boolean DEFAULT true NOT NULL,
    version integer DEFAULT 1 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.ai_interventions (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    parent_type public.support_parent_type NOT NULL,
    parent_id uuid NOT NULL,
    agent_name text NOT NULL,
    tool_name text NOT NULL,
    input_encrypted text,
    output_encrypted text,
    confidence numeric(4,3),
    decision public.ai_intervention_decision NOT NULL,
    tokens_in integer,
    tokens_out integer,
    latency_ms integer,
    cost_usd numeric(10,6),
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    agent_def_version integer
);
--> statement-breakpoint
CREATE TABLE public.alert_channels (
    id text NOT NULL,
    kind text NOT NULL,
    label text NOT NULL,
    enabled boolean DEFAULT false NOT NULL,
    min_severity text DEFAULT 'page'::text NOT NULL,
    config jsonb DEFAULT '{}'::jsonb NOT NULL,
    secret_enc bytea,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT alert_channels_kind_check CHECK ((kind = ANY (ARRAY['email'::text, 'telegram'::text, 'botmaster'::text]))),
    CONSTRAINT alert_channels_min_severity_check CHECK ((min_severity = ANY (ARRAY['warn'::text, 'page'::text])))
);
--> statement-breakpoint
CREATE TABLE public.business_hours (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    vendor_id uuid NOT NULL,
    monday_open time without time zone,
    monday_close time without time zone,
    monday_closed boolean DEFAULT false NOT NULL,
    tuesday_open time without time zone,
    tuesday_close time without time zone,
    tuesday_closed boolean DEFAULT false NOT NULL,
    wednesday_open time without time zone,
    wednesday_close time without time zone,
    wednesday_closed boolean DEFAULT false NOT NULL,
    thursday_open time without time zone,
    thursday_close time without time zone,
    thursday_closed boolean DEFAULT false NOT NULL,
    friday_open time without time zone,
    friday_close time without time zone,
    friday_closed boolean DEFAULT false NOT NULL,
    saturday_open time without time zone,
    saturday_close time without time zone,
    saturday_closed boolean DEFAULT true NOT NULL,
    sunday_open time without time zone,
    sunday_close time without time zone,
    sunday_closed boolean DEFAULT false NOT NULL,
    special_notes text
);
--> statement-breakpoint
CREATE TABLE public.business_types (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    name_he text NOT NULL,
    name_en text NOT NULL,
    sort_order integer DEFAULT 0 NOT NULL,
    is_active boolean DEFAULT true NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.carrier_webhook_event (
    event_id text NOT NULL,
    shipment_id uuid,
    received_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.carrier_webhook_events (
    carrier text NOT NULL,
    event_id text NOT NULL,
    received_at timestamp with time zone DEFAULT now() NOT NULL,
    processed_at timestamp with time zone,
    claimed_at timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE public.cart (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    currency text NOT NULL,
    user_id text,
    created_at timestamp(3) with time zone DEFAULT now() NOT NULL,
    updated_at timestamp(3) with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.cart_checkout_captures (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    checkout_key text NOT NULL,
    order_id uuid NOT NULL,
    order_line_id uuid NOT NULL,
    provider_payment_id text,
    provider_hold_id text NOT NULL,
    amount_agorot integer NOT NULL,
    checkout_order_ids uuid[] NOT NULL,
    status text DEFAULT 'pending'::text NOT NULL,
    capture_idempotency_key text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.cart_checkout_compensations (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    checkout_key text NOT NULL,
    order_id uuid NOT NULL,
    order_line_id uuid NOT NULL,
    provider_payment_id text NOT NULL,
    amount_agorot integer NOT NULL,
    checkout_order_ids uuid[] NOT NULL,
    refund_idempotency_key text NOT NULL,
    status text DEFAULT 'pending'::text NOT NULL,
    provider_refund_id text,
    last_error text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT cart_checkout_compensations_status_ck CHECK ((status = ANY (ARRAY['pending'::text, 'processing'::text, 'refunded'::text, 'cleaning'::text, 'cleanup_completed'::text, 'failed'::text])))
);
--> statement-breakpoint
CREATE TABLE public.cart_items (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    qty integer NOT NULL,
    added_at timestamp with time zone DEFAULT now() NOT NULL,
    deal_sku_id uuid NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.cart_line (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    cart_id uuid NOT NULL,
    variant_id uuid NOT NULL,
    qty integer NOT NULL,
    amount bigint NOT NULL,
    currency text NOT NULL,
    price_mode text NOT NULL,
    vendor_id text
);
--> statement-breakpoint
CREATE TABLE public.case_offers (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    case_id uuid NOT NULL,
    offered_by public.case_offer_by NOT NULL,
    outcome public.case_offer_outcome NOT NULL,
    amount_cents integer,
    reason text,
    status public.case_offer_status DEFAULT 'pending'::public.case_offer_status NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    decided_at timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE public.case_resolutions (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    case_id uuid NOT NULL,
    outcome public.case_offer_outcome NOT NULL,
    amount_cents integer,
    executed_by public.case_resolution_executor NOT NULL,
    executed_at timestamp with time zone DEFAULT now() NOT NULL,
    provider_refund_id text
);
--> statement-breakpoint
CREATE TABLE public.category_translations (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    category_id uuid NOT NULL,
    locale text NOT NULL,
    name text NOT NULL,
    name_source_hash text NOT NULL,
    name_manual_override boolean DEFAULT false NOT NULL,
    model_id text,
    status text DEFAULT 'OK'::text NOT NULL,
    translated_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.chat_messages (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    thread_id uuid NOT NULL,
    sender_user_id uuid NOT NULL,
    body text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.chat_thread_participants (
    thread_id uuid NOT NULL,
    user_id uuid NOT NULL,
    joined_at timestamp with time zone DEFAULT now() NOT NULL,
    last_read_at timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE public.chat_threads (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    title text,
    kind text DEFAULT 'dm'::text NOT NULL,
    created_by uuid NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    last_message_at timestamp with time zone DEFAULT now() NOT NULL,
    last_message_preview character varying(200)
);
--> statement-breakpoint
CREATE TABLE public.club_memberships (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    vendor_id uuid NOT NULL,
    joined_via_order_line_id uuid NOT NULL,
    joined_at timestamp with time zone DEFAULT now() NOT NULL,
    is_active boolean DEFAULT true NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.command_records (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    command_key text NOT NULL,
    command_type text NOT NULL,
    aggregate_type text NOT NULL,
    aggregate_id uuid NOT NULL,
    payload_hash text NOT NULL,
    payload jsonb NOT NULL,
    status text DEFAULT 'CLAIMED'::text NOT NULL,
    claimed_by text NOT NULL,
    claimed_at timestamp with time zone DEFAULT now() NOT NULL,
    completed_at timestamp with time zone,
    failed_at timestamp with time zone,
    result_payload jsonb,
    failure_code text,
    failure_message text,
    claim_generation bigint DEFAULT 1 NOT NULL,
    lease_expires_at timestamp with time zone DEFAULT (CURRENT_TIMESTAMP + '00:10:00'::interval) NOT NULL,
    CONSTRAINT command_records_status_ck CHECK ((status = ANY (ARRAY['CLAIMED'::text, 'COMPLETED'::text, 'FAILED'::text])))
);
--> statement-breakpoint
CREATE TABLE public.credit_ledger (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    amount_agorot bigint NOT NULL,
    entry_type public.credit_entry_type NOT NULL,
    source_type text NOT NULL,
    source_id text NOT NULL,
    referral_id uuid,
    memo text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    mature_at timestamp with time zone NOT NULL,
    swept_at timestamp with time zone,
    resolved_pct smallint,
    withdrawable_at timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE public.cron_cursors (
    job_key text NOT NULL,
    last_processed_at timestamp with time zone NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.cron_runs (
    cron_key text NOT NULL,
    last_success_at timestamp with time zone,
    last_error_at timestamp with time zone,
    last_error text,
    expected_interval_minutes integer DEFAULT 60 NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.deal_annotations (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    deal_id uuid NOT NULL,
    author_id uuid NOT NULL,
    author_role text NOT NULL,
    body text NOT NULL,
    is_visible boolean DEFAULT true NOT NULL,
    created_at timestamp(3) with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.deal_categories (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    deal_type text NOT NULL,
    sort_order integer DEFAULT 0 NOT NULL,
    is_active boolean DEFAULT true NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    slug text NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.deal_drafts (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    vendor_id uuid NOT NULL,
    payload jsonb NOT NULL,
    title text,
    is_starred boolean DEFAULT false NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.deal_images (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    deal_id uuid NOT NULL,
    url text NOT NULL,
    is_primary boolean DEFAULT false NOT NULL,
    sort_order integer DEFAULT 0 NOT NULL,
    approval_status public.image_approval_status DEFAULT 'PENDING'::public.image_approval_status NOT NULL,
    draft_id uuid,
    sku_id uuid
);
--> statement-breakpoint
CREATE TABLE public.deal_item_config (
    deal_id uuid NOT NULL,
    handle_sla_business_days integer NOT NULL,
    shipping_mode public.shipping_mode NOT NULL,
    shipping_flat_agorot integer,
    shipping_free_threshold_agorot integer,
    pickup_enabled boolean DEFAULT false NOT NULL,
    pickup_address_id uuid,
    pickup_hours_json jsonb,
    pickup_instructions text,
    return_window_days integer NOT NULL,
    is_returnable boolean DEFAULT true NOT NULL,
    insurance_required boolean DEFAULT false NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    preferred_carrier text
);
--> statement-breakpoint
CREATE TABLE public.deal_skus (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    deal_id uuid NOT NULL,
    option_ids uuid[] NOT NULL,
    option_ids_hash text NOT NULL,
    original_price numeric(10,2) NOT NULL,
    discount_percent integer NOT NULL,
    discounted_price numeric(10,2) NOT NULL,
    quantity_total integer NOT NULL,
    quantity_sold integer DEFAULT 0 NOT NULL,
    is_active boolean DEFAULT true NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    reserved_qty integer DEFAULT 0 NOT NULL,
    CONSTRAINT sku_discount_percent_range CHECK (((discount_percent >= 0) AND (discount_percent <= 100))),
    CONSTRAINT sku_sold_le_qty CHECK ((quantity_sold <= quantity_total))
);
--> statement-breakpoint
CREATE TABLE public.deal_slug_redirects (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    deal_id uuid NOT NULL,
    locale text NOT NULL,
    old_slug text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.deal_tag_assignments (
    deal_id uuid NOT NULL,
    tag_id uuid NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.deal_tags (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    is_active boolean DEFAULT true NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    slug text NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.deal_translations (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    deal_id uuid NOT NULL,
    locale text NOT NULL,
    slug text NOT NULL,
    title text NOT NULL,
    description text DEFAULT ''::text NOT NULL,
    special_instructions text,
    pickup_address text DEFAULT ''::text NOT NULL,
    title_manual_override boolean DEFAULT false NOT NULL,
    description_manual_override boolean DEFAULT false NOT NULL,
    special_instructions_manual_override boolean DEFAULT false NOT NULL,
    search text,
    model_id text,
    status text DEFAULT 'OK'::text NOT NULL,
    translated_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    title_source_hash text DEFAULT ''::text NOT NULL,
    description_source_hash text DEFAULT ''::text NOT NULL,
    special_instructions_source_hash text
);
--> statement-breakpoint
CREATE TABLE public.deal_variant_axes (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    deal_id uuid NOT NULL,
    axis_order integer NOT NULL,
    kind public.variant_axis_kind NOT NULL,
    name_he text NOT NULL,
    name_en text NOT NULL,
    is_active boolean DEFAULT true NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    is_visual_axis boolean DEFAULT false NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.deal_variant_options (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    axis_id uuid NOT NULL,
    option_order integer NOT NULL,
    value_code text NOT NULL,
    label_he text NOT NULL,
    label_en text NOT NULL,
    slot_start timestamp with time zone,
    slot_end timestamp with time zone,
    swatch_hex text,
    is_active boolean DEFAULT true NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.deals (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    vendor_id uuid NOT NULL,
    deal_type public.deal_type NOT NULL,
    title text NOT NULL,
    description text DEFAULT ''::text NOT NULL,
    category text,
    window_start timestamp with time zone,
    window_end timestamp with time zone,
    pickup_start time without time zone,
    pickup_end time without time zone,
    pickup_address text DEFAULT ''::text NOT NULL,
    special_instructions text,
    deal_state public.deal_state DEFAULT 'DRAFT'::public.deal_state NOT NULL,
    rejection_reason text,
    rejection_detail text,
    commission_rate numeric(4,3) DEFAULT 0.100 NOT NULL,
    is_personal_deal boolean DEFAULT false NOT NULL,
    personal_deal_for_user_id uuid,
    approved_at timestamp with time zone,
    approved_by public.approved_by,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    sold_out_at timestamp with time zone,
    sold_out_gold_expires_at timestamp with time zone,
    content_hash text,
    appeal_status public.appeal_status,
    appeal_reason text,
    appeal_at timestamp with time zone,
    appeal_decided_at timestamp with time zone,
    sla_alerted_at timestamp with time zone,
    max_per_user integer,
    category_id uuid,
    is_voucher boolean DEFAULT false NOT NULL,
    source_language text DEFAULT 'he'::text NOT NULL,
    translation_status text DEFAULT 'NOT_STARTED'::text NOT NULL,
    metadata jsonb DEFAULT '{}'::jsonb NOT NULL,
    min_price numeric(10,2),
    max_price numeric(10,2),
    max_discount_percent integer,
    stock_remaining integer,
    return_window_days integer DEFAULT 14 NOT NULL,
    is_returnable boolean DEFAULT true NOT NULL,
    is_physical boolean DEFAULT false NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.dlq_events (
    queue_key text NOT NULL,
    dead_count bigint DEFAULT 0 NOT NULL,
    last_dead_at timestamp with time zone,
    last_reason text,
    acknowledged_count bigint DEFAULT 0 NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.e2e_factory_commands (
    run_id uuid NOT NULL,
    request_id uuid NOT NULL,
    body_hash text NOT NULL,
    operation text NOT NULL,
    result jsonb,
    completed_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.e2e_factory_entities (
    run_id uuid NOT NULL,
    kind text NOT NULL,
    entity_id uuid NOT NULL,
    operation text NOT NULL,
    dependency_order integer NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE public.e2e_factory_runs (
    run_id uuid NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    touched_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.email_verifications (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    token_hash text NOT NULL,
    email_encrypted text NOT NULL,
    purpose public.email_verification_purpose NOT NULL,
    expires_at timestamp with time zone NOT NULL,
    consumed_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.favorite_vendors (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    vendor_id uuid NOT NULL,
    notify boolean DEFAULT false NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.fraud_events (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    referral_id uuid,
    decision_point text NOT NULL,
    adapter text NOT NULL,
    action text NOT NULL,
    codes text[] DEFAULT '{}'::text[] NOT NULL,
    detail jsonb,
    resolved_by uuid,
    resolved_at timestamp with time zone,
    ts timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.group_deals (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    deal_id uuid NOT NULL,
    fill_rule public.fill_rule NOT NULL,
    min_group_size integer NOT NULL,
    max_group_size integer NOT NULL,
    per_customer_limit integer DEFAULT 1 NOT NULL,
    cancellation_policy public.cancellation_policy DEFAULT 'LEGAL_ONLY'::public.cancellation_policy NOT NULL,
    cancellation_window_hours integer,
    tiered_pricing_enabled boolean DEFAULT false NOT NULL,
    early_bird_enabled boolean DEFAULT false NOT NULL,
    early_bird_slots integer,
    early_bird_discount_percent integer,
    social_sharing_enabled boolean DEFAULT false NOT NULL,
    social_sharing_reward text,
    bulk_pickup_enabled boolean DEFAULT false NOT NULL,
    bulk_pickup_details text,
    group_state public.group_deal_state DEFAULT 'COLLECTING'::public.group_deal_state NOT NULL,
    current_reservation_count integer DEFAULT 0 NOT NULL,
    threshold_met_at timestamp with time zone,
    extended_deadline timestamp with time zone,
    extension_count integer DEFAULT 0 NOT NULL,
    partial_decision_deadline timestamp with time zone,
    vendor_honored_at timestamp with time zone,
    executed_at timestamp with time zone,
    failed_at timestamp with time zone,
    source_group_deal_id uuid,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT group_deals_min_max CHECK ((min_group_size <= max_group_size)),
    CONSTRAINT group_deals_min_positive CHECK ((min_group_size >= 2))
);
--> statement-breakpoint
CREATE TABLE public.group_reservations (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    group_deal_id uuid NOT NULL,
    deal_id uuid NOT NULL,
    user_id uuid,
    guest_email text,
    guest_phone text,
    quantity integer DEFAULT 1 NOT NULL,
    unit_price numeric(10,2) NOT NULL,
    total_amount numeric(10,2) NOT NULL,
    commission_amount numeric(10,2) NOT NULL,
    vendor_amount numeric(10,2) NOT NULL,
    status public.reservation_status DEFAULT 'HELD'::public.reservation_status NOT NULL,
    payment_method_id uuid,
    provider_authorization_id text,
    provider_transaction_id text,
    hold_expires_at timestamp with time zone,
    hold_refreshed_at timestamp with time zone,
    is_early_bird boolean DEFAULT false NOT NULL,
    order_line_id uuid,
    cancelled_at timestamp with time zone,
    cancellation_reason text,
    idempotency_key text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    mock_scenario text,
    provider_card_token text
);
--> statement-breakpoint
CREATE TABLE public.group_tiers (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    group_deal_id uuid NOT NULL,
    min_participants integer NOT NULL,
    price_per_unit numeric(10,2) NOT NULL,
    discount_percent integer NOT NULL,
    sort_order integer DEFAULT 0 NOT NULL,
    CONSTRAINT group_tiers_discount_range CHECK (((discount_percent >= 0) AND (discount_percent <= 100)))
);
--> statement-breakpoint
CREATE TABLE public.group_waitlist (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    group_deal_id uuid NOT NULL,
    user_id uuid,
    guest_email text,
    guest_phone text,
    quantity integer DEFAULT 1 NOT NULL,
    payment_method_id uuid,
    "position" integer NOT NULL,
    promoted_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.image_uploads (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    uploader_user_id uuid,
    uploader_vendor_id uuid,
    r2_key text NOT NULL,
    mime text NOT NULL,
    size_bytes integer NOT NULL,
    scan_status public.scan_status DEFAULT 'PENDING'::public.scan_status NOT NULL,
    approval_status public.image_approval_status DEFAULT 'PENDING'::public.image_approval_status NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    reject_reason_code text,
    purpose public.upload_purpose NOT NULL,
    entity_type public.image_entity_type,
    entity_id uuid,
    ai_decision public.ai_decision,
    ai_reason text,
    ai_score numeric(3,2),
    ai_model text,
    ai_checked_at timestamp with time zone,
    ai_raw_payload jsonb
);
--> statement-breakpoint
CREATE TABLE public.inventory_item (
    sku_id uuid NOT NULL,
    vendor_id text,
    quantity_total integer,
    quantity_sold integer DEFAULT 0 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.invoices (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    order_line_id uuid NOT NULL,
    vendor_id uuid NOT NULL,
    buyer_id uuid NOT NULL,
    provider public.invoice_provider NOT NULL,
    provider_document_id text,
    provider_document_number text,
    provider_document_url text,
    status text NOT NULL,
    failure_reason text,
    retry_count integer DEFAULT 0 NOT NULL,
    gross_agorot integer NOT NULL,
    vat_agorot integer NOT NULL,
    net_agorot integer NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.kyc_verifications (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    status public.kyc_status DEFAULT 'none'::public.kyc_status NOT NULL,
    submitted_at timestamp with time zone,
    verified_at timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE public.languages (
    code text NOT NULL,
    name_native text NOT NULL,
    name_english text NOT NULL,
    direction text NOT NULL,
    search_config text NOT NULL,
    is_active boolean DEFAULT true NOT NULL,
    is_default boolean DEFAULT false NOT NULL,
    sort_order integer DEFAULT 0 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.ledger_entries (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    delta bigint NOT NULL,
    reason text NOT NULL,
    ref jsonb,
    idempotency_key text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    currency text
);
--> statement-breakpoint
CREATE TABLE public.ledger_entry_vesting (
    entry_id uuid NOT NULL,
    mature_at timestamp with time zone,
    swept_at timestamp with time zone,
    withdrawable_at timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE public.ledger_reconciliation_repairs (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    expected_balance_agorot bigint NOT NULL,
    actual_balance_agorot bigint NOT NULL,
    balance_delta_agorot bigint NOT NULL,
    expected_lifetime_agorot bigint NOT NULL,
    actual_lifetime_agorot bigint NOT NULL,
    repaired_columns text NOT NULL,
    recurrence boolean DEFAULT false NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.live_notifications (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    event text NOT NULL,
    tier text NOT NULL,
    title_he text NOT NULL,
    title_en text NOT NULL,
    body_he text,
    body_en text,
    link text,
    payload jsonb DEFAULT '{}'::jsonb NOT NULL,
    read_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    expires_at timestamp with time zone DEFAULT (now() + '30 days'::interval) NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.llm_jobs (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    job_type public.llm_job_type NOT NULL,
    target_id uuid NOT NULL,
    target_type public.llm_target_type NOT NULL,
    status public.llm_job_status DEFAULT 'PENDING'::public.llm_job_status NOT NULL,
    input_payload jsonb NOT NULL,
    output_payload jsonb,
    prompt_snapshot text,
    decision public.llm_decision,
    flag_reason text,
    started_at timestamp with time zone,
    completed_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    retry_count integer DEFAULT 0 NOT NULL,
    last_error text,
    prompt_tokens integer,
    completion_tokens integer,
    total_tokens integer,
    cost_usd numeric(10,6),
    model_name text,
    target_locale text,
    job_class text DEFAULT 'BATCH'::text NOT NULL,
    not_before timestamp with time zone,
    model_used text,
    e2e_run_id uuid,
    queue_chain_override jsonb
);
--> statement-breakpoint
CREATE TABLE public.llm_model_cache (
    provider_id uuid NOT NULL,
    models jsonb NOT NULL,
    fetched_at timestamp with time zone DEFAULT now() NOT NULL,
    error text
);
--> statement-breakpoint
CREATE TABLE public.llm_provider_configs (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    adapter text NOT NULL,
    label text NOT NULL,
    api_key text NOT NULL,
    base_url text,
    is_active boolean DEFAULT true NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.llm_providers (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    slug text NOT NULL,
    name text NOT NULL,
    type public.llm_provider_type NOT NULL,
    base_url text,
    api_key_enc bytea,
    is_active boolean DEFAULT true NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.llm_queues (
    id text NOT NULL,
    name text NOT NULL,
    chain jsonb DEFAULT '[]'::jsonb NOT NULL,
    prompt text DEFAULT ''::text NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_by uuid
);
--> statement-breakpoint
CREATE TABLE public.loyalty_memberships (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    vendor_id uuid NOT NULL,
    joined_via_purchase_id uuid NOT NULL,
    joined_at timestamp with time zone DEFAULT now() NOT NULL,
    is_active boolean DEFAULT true NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.magic_links (
    id text NOT NULL,
    token_hash text NOT NULL,
    email_encrypted text NOT NULL,
    pre_fill jsonb,
    used_at timestamp with time zone,
    expires_at timestamp with time zone NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.marketing_trigger_state (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    trigger_key text NOT NULL,
    fired_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.mock_payment_events (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    op text NOT NULL,
    order_line_id uuid,
    reservation_id uuid,
    vendor_id uuid,
    scenario text NOT NULL,
    outcome_code text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.monitor_checks (
    key text NOT NULL,
    enabled boolean DEFAULT true NOT NULL,
    config jsonb DEFAULT '{}'::jsonb NOT NULL,
    severity_floor text,
    interval_hours integer,
    include_in_digest boolean DEFAULT true NOT NULL,
    last_run_at timestamp with time zone,
    last_severity text,
    last_detail text,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT monitor_checks_severity_floor_check CHECK ((severity_floor = ANY (ARRAY['info'::text, 'warn'::text, 'page'::text])))
);
--> statement-breakpoint
CREATE TABLE public.notifications (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    recipient_type text NOT NULL,
    recipient_id uuid NOT NULL,
    event_type text NOT NULL,
    severity public.notification_severity DEFAULT 'warning'::public.notification_severity NOT NULL,
    payload jsonb NOT NULL,
    read_at timestamp with time zone,
    resolved_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.ops_alert_state (
    alert_key text NOT NULL,
    last_fired_at timestamp with time zone NOT NULL,
    last_severity text NOT NULL
);
--> statement-breakpoint
CREATE TABLE public."order" (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    buyer_user_id uuid,
    buyer_guest_email text,
    status text DEFAULT 'pending'::text NOT NULL,
    currency text NOT NULL,
    price_mode text NOT NULL,
    subtotal bigint NOT NULL,
    tax bigint NOT NULL,
    discount bigint NOT NULL,
    total bigint NOT NULL,
    charge_ref text,
    failure_reason text,
    idempotency_key text NOT NULL,
    request_hash text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT order_buyer_chk CHECK ((num_nonnulls(buyer_user_id, buyer_guest_email) = 1))
);
--> statement-breakpoint
CREATE TABLE public.order_guest_contact (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    order_id uuid NOT NULL,
    email_enc text NOT NULL,
    phone_enc text,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.order_line (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    order_id uuid NOT NULL,
    variant_id uuid NOT NULL,
    kind text NOT NULL,
    qty integer NOT NULL,
    unit_price bigint NOT NULL,
    line_total bigint NOT NULL,
    vendor_id text,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.order_line_voucher_ext (
    voucher_id text NOT NULL,
    line_id text NOT NULL,
    qr_token_hash text,
    qr_png_url text,
    review_eligible boolean DEFAULT false NOT NULL,
    guest_access_token_hash text,
    guest_access_token_expires_at timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE public.order_payment_fees (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    order_id uuid NOT NULL,
    kind text DEFAULT 'charge'::text NOT NULL,
    stripe_fee_agorot integer,
    provider_payment_id text,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.order_share_attribution (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    order_id uuid NOT NULL,
    share_slug_id uuid NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.order_step (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    order_id uuid NOT NULL,
    step_id text NOT NULL,
    result jsonb NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.outbox (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    aggregate_type text NOT NULL,
    aggregate_id uuid NOT NULL,
    event_type text NOT NULL,
    payload jsonb NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    processed_at timestamp with time zone,
    failed_at timestamp with time zone,
    retry_count integer DEFAULT 0 NOT NULL,
    last_error text,
    dead_at timestamp with time zone,
    retry_limit integer DEFAULT 3 NOT NULL,
    dedupe_key text
);
--> statement-breakpoint
CREATE TABLE public.page_layout_pointers (
    page text NOT NULL,
    live_version_id uuid NOT NULL,
    draft_body jsonb,
    draft_updated_at timestamp with time zone,
    draft_updated_by uuid,
    scheduled_version_id uuid,
    scheduled_at timestamp with time zone,
    page_metadata jsonb
);
--> statement-breakpoint
CREATE TABLE public.page_layouts (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    page text NOT NULL,
    version integer NOT NULL,
    body jsonb NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    created_by uuid,
    note text DEFAULT ''::text NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.payment_expected_contracts (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    purchase_id uuid NOT NULL,
    order_id uuid NOT NULL,
    amount_agorot integer NOT NULL,
    currency text NOT NULL,
    customer_id text,
    destination_account_id text NOT NULL,
    application_fee_agorot integer NOT NULL,
    effect_key text NOT NULL,
    binding_source text DEFAULT 'checkout'::text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.payment_finalize_checkpoints (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    purchase_id uuid NOT NULL,
    order_id uuid NOT NULL,
    provider_payment_id text NOT NULL,
    effect_key text NOT NULL,
    status text DEFAULT 'pending'::text NOT NULL,
    attempt integer DEFAULT 0 NOT NULL,
    lease_owner text,
    lease_expires_at timestamp with time zone,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    completed_at timestamp with time zone,
    result jsonb,
    CONSTRAINT payment_finalize_checkpoints_status_check CHECK ((status = ANY (ARRAY['pending'::text, 'running'::text, 'completed'::text])))
);
--> statement-breakpoint
CREATE TABLE public.payment_methods (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    last4 text NOT NULL,
    brand public.payment_brand NOT NULL,
    token text NOT NULL,
    is_default boolean DEFAULT false NOT NULL,
    card_fingerprint text
);
--> statement-breakpoint
CREATE TABLE public.payout_audit_log (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    kind text NOT NULL,
    vendor_id uuid,
    stripe_balance_agorot integer,
    held_liability_agorot integer,
    drift_agorot integer,
    payout_id text,
    refund_id text,
    note text,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.payouts (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    vendor_id uuid NOT NULL,
    period_month text NOT NULL,
    amount_agorot integer NOT NULL,
    platform_fee_agorot integer NOT NULL,
    status text DEFAULT 'pending'::text NOT NULL,
    marked_paid_at timestamp with time zone,
    marked_paid_by text,
    notes text,
    stripe_payout_id text,
    stripe_transfer_id text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.personal_deal_requests (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    vendor_id uuid NOT NULL,
    source_deal_id uuid NOT NULL,
    status public.personal_deal_status DEFAULT 'PENDING'::public.personal_deal_status NOT NULL,
    response_deadline timestamp with time zone NOT NULL,
    responded_at timestamp with time zone,
    created_deal_id uuid,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.platform_image_limits (
    id integer DEFAULT 1 NOT NULL,
    max_images_per_deal integer DEFAULT 10 NOT NULL,
    max_images_per_sku integer DEFAULT 10 NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_by_user_id uuid,
    CONSTRAINT platform_image_limits_singleton CHECK ((id = 1))
);
--> statement-breakpoint
CREATE TABLE public.promo (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    code text NOT NULL,
    kind text NOT NULL,
    value_bps integer,
    value_amount bigint,
    currency text,
    bogo_buy_qty integer,
    bogo_get_qty integer,
    scope jsonb NOT NULL,
    eligibility jsonb NOT NULL,
    funder text NOT NULL,
    max_uses integer,
    per_user_cap integer,
    uses integer DEFAULT 0 NOT NULL,
    starts_at timestamp with time zone,
    ends_at timestamp with time zone,
    min_order_amount bigint,
    active boolean DEFAULT true NOT NULL,
    vendor_id text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    max_discount_amount bigint,
    max_order_amount bigint,
    author_user_id uuid,
    status text DEFAULT 'active'::text NOT NULL,
    valid_from timestamp with time zone,
    valid_until timestamp with time zone,
    min_subtotal integer,
    max_subtotal integer,
    total_cap integer,
    redemption_count integer DEFAULT 0 NOT NULL,
    rules_json jsonb DEFAULT '{}'::jsonb NOT NULL,
    description text,
    max_cap_amount integer,
    bogo_buy integer,
    bogo_get_free integer,
    CONSTRAINT promo_max_uses_chk CHECK (((max_uses IS NULL) OR (max_uses > 0))),
    CONSTRAINT promo_per_user_cap_chk CHECK (((per_user_cap IS NULL) OR (per_user_cap > 0))),
    CONSTRAINT promo_value_amount_chk CHECK (((value_amount IS NULL) OR (value_amount > 0))),
    CONSTRAINT promo_value_bps_chk CHECK (((value_bps IS NULL) OR ((value_bps > 0) AND (value_bps <= 10000))))
);
--> statement-breakpoint
CREATE TABLE public.promo_allowlist (
    promo_id uuid NOT NULL,
    user_id uuid NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.promo_redemptions (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    promo_code_id uuid NOT NULL,
    order_line_id uuid NOT NULL,
    user_id uuid NOT NULL,
    vendor_id uuid,
    funder text NOT NULL,
    discount_amount integer NOT NULL,
    applied_to_lines jsonb NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT promo_redemptions_funder_check CHECK ((funder = ANY (ARRAY['platform'::text, 'vendor'::text])))
);
--> statement-breakpoint
CREATE TABLE public.promo_reservations (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    promo_code_id uuid NOT NULL,
    user_id uuid NOT NULL,
    purchase_id uuid NOT NULL,
    order_line_id uuid NOT NULL,
    vendor_id uuid NOT NULL,
    checkout_scope text NOT NULL,
    currency text NOT NULL,
    snapshot jsonb NOT NULL,
    quota_owner boolean DEFAULT false NOT NULL,
    discount_agorot integer NOT NULL,
    funder text NOT NULL,
    status text DEFAULT 'pending'::text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT promo_reservations_discount_agorot_check CHECK ((discount_agorot > 0)),
    CONSTRAINT promo_reservations_funder_check CHECK ((funder = ANY (ARRAY['platform'::text, 'vendor'::text]))),
    CONSTRAINT promo_reservations_status_check CHECK ((status = ANY (ARRAY['pending'::text, 'finalized'::text, 'failed'::text])))
);
--> statement-breakpoint
CREATE TABLE public.promo_user_usage (
    promo_id uuid NOT NULL,
    user_id text NOT NULL,
    uses integer DEFAULT 0 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.purchase_messages (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    order_line_id uuid NOT NULL,
    sender_type public.sender_type NOT NULL,
    sender_id uuid NOT NULL,
    body text NOT NULL,
    read_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.purchases_archived_pre_wipe (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid,
    guest_email text,
    guest_phone text,
    deal_id uuid NOT NULL,
    vendor_id uuid NOT NULL,
    quantity integer DEFAULT 1 NOT NULL,
    amount_paid numeric(10,2) NOT NULL,
    commission_amount numeric(10,2) NOT NULL,
    vendor_amount numeric(10,2) NOT NULL,
    payment_method_id uuid,
    payment_status public.payment_status DEFAULT 'PENDING'::public.payment_status NOT NULL,
    redemption_status public.redemption_status DEFAULT 'UNREDEEMED'::public.redemption_status NOT NULL,
    qr_token_hash text NOT NULL,
    qr_png_url text,
    redeemed_at timestamp with time zone,
    expires_at timestamp with time zone NOT NULL,
    cancelled_at timestamp with time zone,
    cancellation_reason text,
    review_eligible boolean DEFAULT false NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    idempotency_key text NOT NULL,
    platform_fee_amount_agorot integer,
    platform_fee_pct integer,
    guest_access_token_hash text,
    guest_access_token_expires_at timestamp with time zone,
    mock_scenario text,
    provider_payment_id text,
    provider_refund_id text,
    vendor_tax_doc_id text,
    platform_tax_doc_id text,
    vendor_tax_doc_pdf_url text,
    platform_tax_doc_pdf_url text,
    metadata jsonb DEFAULT '{}'::jsonb NOT NULL,
    deal_sku_id uuid,
    ref_attribution_id uuid,
    shipped_at timestamp with time zone,
    delivered_at timestamp with time zone,
    share_slug_id uuid,
    stripe_fee_agorot integer
);
--> statement-breakpoint
CREATE TABLE public.push_subscriptions (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    endpoint text NOT NULL,
    p256dh text NOT NULL,
    auth text NOT NULL,
    user_agent text DEFAULT ''::text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    revoked_at timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE public.rate_limit_buckets (
    key text NOT NULL,
    count integer DEFAULT 0 NOT NULL,
    window_start_at timestamp with time zone NOT NULL,
    refilled_at timestamp with time zone NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.referral_link_stats_daily (
    link_id uuid NOT NULL,
    day date NOT NULL,
    clicks integer DEFAULT 0 NOT NULL,
    clicks_suspicious integer DEFAULT 0 NOT NULL,
    signups integer DEFAULT 0 NOT NULL,
    purchases integer DEFAULT 0 NOT NULL,
    commission_agorot bigint DEFAULT 0 NOT NULL,
    reward_agorot bigint DEFAULT 0 NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.referral_links (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    owner_user_id uuid NOT NULL,
    code text NOT NULL,
    kind public.referral_kind DEFAULT 'referral'::public.referral_kind NOT NULL,
    commission_pct_override integer,
    commission_window_days_override integer,
    active boolean DEFAULT true NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.referral_settings (
    id integer NOT NULL,
    affiliate_pct integer DEFAULT 30 NOT NULL,
    affiliate_window_days integer DEFAULT 90 NOT NULL,
    affiliate_max_orders integer DEFAULT 50 NOT NULL,
    cookie_days integer DEFAULT 14 NOT NULL,
    reward_agorot integer DEFAULT 2000 NOT NULL,
    referee_discount_agorot integer DEFAULT 2000 NOT NULL,
    hold_days integer DEFAULT 30 NOT NULL,
    withdrawal_min_agorot integer DEFAULT 30000 NOT NULL,
    auto_approve_lifetime_agorot integer DEFAULT 50000 NOT NULL,
    brand_keyword_blocklist text[] DEFAULT ARRAY['multideal'::text, 'מולטידיל'::text, 'multi.deal'::text] NOT NULL,
    disallowed_referer_hosts text[] DEFAULT '{}'::text[] NOT NULL,
    tos_version text DEFAULT 'v1-2026-05-28'::text NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_by_user_id uuid,
    tier1_pct smallint DEFAULT 3 NOT NULL,
    tier2_pct smallint DEFAULT 4 NOT NULL,
    tier3_pct smallint DEFAULT 5 NOT NULL,
    tier2_min_sales integer DEFAULT 20 NOT NULL,
    tier3_min_sales integer DEFAULT 50 NOT NULL,
    referral_pct smallint DEFAULT 2 NOT NULL,
    fraud_config jsonb DEFAULT '{}'::jsonb NOT NULL,
    dispute_window_days integer DEFAULT 120 NOT NULL,
    reserve_pct integer DEFAULT 0 NOT NULL,
    reserve_release_days integer DEFAULT 120 NOT NULL,
    CONSTRAINT referral_pct_below_fee CHECK (((referral_pct >= 1) AND (referral_pct < 10))),
    CONSTRAINT referral_settings_id_check CHECK ((id = 1)),
    CONSTRAINT referral_settings_tier1_pct_check CHECK (((tier1_pct >= 1) AND (tier1_pct <= 10))),
    CONSTRAINT referral_settings_tier2_pct_check CHECK (((tier2_pct >= 1) AND (tier2_pct <= 10))),
    CONSTRAINT referral_settings_tier3_pct_check CHECK (((tier3_pct >= 1) AND (tier3_pct <= 10)))
);
--> statement-breakpoint
CREATE TABLE public.referrals (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    referrer_user_id uuid NOT NULL,
    referee_user_id uuid NOT NULL,
    link_id uuid NOT NULL,
    kind public.referral_kind NOT NULL,
    status public.referral_status DEFAULT 'pending'::public.referral_status NOT NULL,
    qualified_at timestamp with time zone,
    commission_pct integer,
    commission_window_ends_at timestamp with time zone,
    commission_orders_remaining integer,
    referee_promo_code_id uuid,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    visitor_id_referrer text,
    visitor_id_referee text,
    ip_hash_referee text,
    quarantined_at timestamp with time zone,
    clicked_at timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE public.refund_intent (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    order_id uuid NOT NULL,
    seq integer NOT NULL,
    amount bigint NOT NULL,
    status text DEFAULT 'pending'::text NOT NULL,
    refund_key text NOT NULL,
    provider_ref text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT refund_intent_amount_chk CHECK ((amount > 0))
);
--> statement-breakpoint
CREATE TABLE public.refund_intent_line_ext (
    refund_intent_id uuid NOT NULL,
    order_line_id uuid NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.refunds (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    purchase_id uuid NOT NULL,
    seq integer NOT NULL,
    amount_agorot integer NOT NULL,
    provider_refund_id text,
    status text DEFAULT 'pending'::text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    completed_at timestamp with time zone,
    CONSTRAINT refunds_amount_agorot_check CHECK ((amount_agorot > 0)),
    CONSTRAINT refunds_status_check CHECK ((status = ANY (ARRAY['pending'::text, 'completed'::text, 'failed'::text])))
);
--> statement-breakpoint
CREATE TABLE public.report_tickets (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    reporter_user_id uuid,
    target_type public.report_target_type NOT NULL,
    target_id uuid NOT NULL,
    reason public.report_reason NOT NULL,
    body text DEFAULT ''::text NOT NULL,
    status public.report_status DEFAULT 'OPEN'::public.report_status NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    resolved_at timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE public.return_events (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    return_id uuid NOT NULL,
    from_status public.return_status,
    to_status public.return_status NOT NULL,
    actor_type text NOT NULL,
    actor_id uuid,
    reason text,
    metadata jsonb DEFAULT '{}'::jsonb NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.returns (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    order_line_id uuid NOT NULL,
    buyer_id uuid NOT NULL,
    vendor_id uuid NOT NULL,
    deal_id uuid NOT NULL,
    status public.return_status DEFAULT 'requested'::public.return_status NOT NULL,
    reason public.return_reason NOT NULL,
    reason_note text,
    liability public.return_liability NOT NULL,
    refund_amount_agorot integer NOT NULL,
    restocking_fee_agorot integer DEFAULT 0 NOT NULL,
    shipping_reimbursement_agorot integer DEFAULT 0 NOT NULL,
    cash_refund_agorot integer,
    credit_overflow_agorot integer DEFAULT 0 NOT NULL,
    carrier text,
    tracking_number text,
    tracking_entered_at timestamp with time zone,
    received_at timestamp with time zone,
    vendor_respond_by timestamp with time zone,
    buyer_ship_by timestamp with time zone,
    vendor_inspect_by timestamp with time zone,
    provider_refund_id text,
    case_id uuid,
    metadata jsonb DEFAULT '{}'::jsonb NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    closed_at timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE public.review (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    product_id text NOT NULL,
    user_id text NOT NULL,
    purchase_id text NOT NULL,
    vendor_id text,
    rating integer NOT NULL,
    body text,
    status text DEFAULT 'pending'::text NOT NULL,
    vendor_reply text,
    created_at timestamp(3) with time zone DEFAULT now() NOT NULL,
    updated_at timestamp(3) with time zone DEFAULT now() NOT NULL,
    CONSTRAINT review_body_check CHECK (((body IS NULL) OR (length(body) <= 5000))),
    CONSTRAINT review_rating_check CHECK (((rating >= 1) AND (rating <= 5))),
    CONSTRAINT review_status_check CHECK ((status = ANY (ARRAY['pending'::text, 'approved'::text, 'rejected'::text]))),
    CONSTRAINT review_vendor_reply_check CHECK (((vendor_reply IS NULL) OR (length(vendor_reply) <= 5000)))
);
--> statement-breakpoint
CREATE TABLE public.review_removal_queue (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    review_id uuid NOT NULL,
    submitted_by uuid NOT NULL,
    reason text NOT NULL,
    ai_score numeric(5,4),
    status public.review_removal_queue_status DEFAULT 'PENDING'::public.review_removal_queue_status NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    decided_at timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE public.review_translations (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    review_id uuid NOT NULL,
    locale text NOT NULL,
    body text DEFAULT ''::text NOT NULL,
    status text DEFAULT 'OK'::text NOT NULL,
    source_hash text DEFAULT ''::text NOT NULL,
    model_id text,
    translated_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.reviews (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    order_line_id uuid NOT NULL,
    user_id uuid NOT NULL,
    vendor_id uuid NOT NULL,
    review_type public.review_type DEFAULT 'STANDARD'::public.review_type NOT NULL,
    rating integer,
    body text NOT NULL,
    vendor_reply text,
    vendor_reply_at timestamp with time zone,
    removal_requested boolean DEFAULT false NOT NULL,
    removal_reason text,
    removal_status public.removal_status DEFAULT 'NONE'::public.removal_status NOT NULL,
    removed_by public.removed_by,
    is_visible boolean DEFAULT true NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    language text DEFAULT 'und'::text NOT NULL,
    deal_id uuid NOT NULL,
    CONSTRAINT reviews_rating_range CHECK (((rating IS NULL) OR ((rating >= 1) AND (rating <= 5))))
);
--> statement-breakpoint
CREATE TABLE public.sessions (
    id text NOT NULL,
    user_id uuid NOT NULL,
    csrf_token text NOT NULL,
    user_agent text DEFAULT ''::text NOT NULL,
    ip_encrypted text,
    expires_at timestamp with time zone NOT NULL,
    revoked_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    refresh_token_hash text NOT NULL,
    last_refreshed_at timestamp with time zone,
    previous_refresh_token_hash text
);
--> statement-breakpoint
CREATE TABLE public.share_audience_daily (
    day date NOT NULL,
    device text DEFAULT ''::text NOT NULL,
    country text DEFAULT ''::text NOT NULL,
    clicks integer DEFAULT 0 NOT NULL,
    clicks_suspicious integer DEFAULT 0 NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.share_link_stats_daily (
    link_id uuid NOT NULL,
    day date NOT NULL,
    clicks integer DEFAULT 0 NOT NULL,
    clicks_suspicious integer DEFAULT 0 NOT NULL,
    deal_id uuid,
    channel public.share_channel
);
--> statement-breakpoint
CREATE TABLE public.share_links (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    slug text NOT NULL,
    target_url text NOT NULL,
    deal_id uuid,
    owner_user_id uuid,
    channel public.share_channel DEFAULT 'other'::public.share_channel NOT NULL,
    campaign_name text,
    utm_source text NOT NULL,
    utm_medium text DEFAULT 'social'::text NOT NULL,
    utm_campaign text NOT NULL,
    is_active boolean DEFAULT true NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    expires_at timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE public.shipment (
    id uuid NOT NULL,
    order_id uuid NOT NULL,
    line_ids jsonb NOT NULL,
    address jsonb NOT NULL,
    status text DEFAULT 'pending'::text NOT NULL,
    carrier_kind text,
    label_id text,
    tracking_number text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT shipment_status_chk CHECK ((status = ANY (ARRAY['pending'::text, 'labeled'::text, 'in_transit'::text, 'delivered'::text, 'failed'::text])))
);
--> statement-breakpoint
CREATE TABLE public.shipment_events (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    shipment_id uuid NOT NULL,
    from_status public.shipment_status,
    to_status public.shipment_status NOT NULL,
    source text NOT NULL,
    actor_id uuid,
    carrier_raw_json jsonb,
    note text,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.shipment_purchases (
    shipment_id uuid NOT NULL,
    order_line_id uuid NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.shipments (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    vendor_id uuid NOT NULL,
    buyer_id uuid NOT NULL,
    shipping_address_id uuid,
    pickup_address_id uuid,
    status public.shipment_status DEFAULT 'pending'::public.shipment_status NOT NULL,
    carrier public.carrier,
    tracking_number text,
    tracking_url text,
    carrier_label_r2_key text,
    packing_slip_r2_key text,
    pickup_code text,
    pickup_code_expires_at timestamp with time zone,
    handle_by_at timestamp with time zone NOT NULL,
    shipped_at timestamp with time zone,
    delivered_at timestamp with time zone,
    confirmed_at timestamp with time zone,
    auto_confirm_at timestamp with time zone,
    settlement_at timestamp with time zone,
    stripe_transfer_id text,
    shipping_charged_agorot integer DEFAULT 0 NOT NULL,
    insurance_purchased boolean DEFAULT false NOT NULL,
    insurance_cost_agorot integer DEFAULT 0 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.shipping_addresses (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    recipient_name text NOT NULL,
    recipient_phone text NOT NULL,
    city_code text NOT NULL,
    city_name text NOT NULL,
    street_code text,
    street_name text NOT NULL,
    house_number text NOT NULL,
    apt text,
    entrance text,
    floor text,
    zip text NOT NULL,
    zip_validated boolean DEFAULT false NOT NULL,
    notes text,
    lat numeric(10,7),
    lng numeric(10,7),
    is_default boolean DEFAULT false NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.sku_qty_tiers (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    deal_sku_id uuid NOT NULL,
    min_qty integer NOT NULL,
    discount_percent integer NOT NULL,
    sort_order integer DEFAULT 0 NOT NULL,
    CONSTRAINT sku_qty_tiers_discount_range CHECK (((discount_percent >= 1) AND (discount_percent <= 100))),
    CONSTRAINT sku_qty_tiers_min_qty CHECK ((min_qty >= 2))
);
--> statement-breakpoint
CREATE TABLE public.slug_history (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    entity_type text NOT NULL,
    entity_id uuid NOT NULL,
    old_slug text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT slug_history_entity_type_check CHECK ((entity_type = ANY (ARRAY['tag'::text, 'category'::text])))
);
--> statement-breakpoint
CREATE TABLE public.standing_offers (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    vendor_id uuid NOT NULL,
    title text NOT NULL,
    description text DEFAULT ''::text NOT NULL,
    image_url text,
    original_price numeric(10,2) NOT NULL,
    discount_percent integer DEFAULT 0 NOT NULL,
    discounted_price numeric(10,2) NOT NULL,
    is_active boolean DEFAULT true NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT standing_offers_discount_range CHECK (((discount_percent >= 0) AND (discount_percent <= 100)))
);
--> statement-breakpoint
CREATE TABLE public.stock_reservation (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    sku_id uuid NOT NULL,
    qty integer NOT NULL,
    holder_ref text NOT NULL,
    expires_at timestamp with time zone NOT NULL,
    consumed_at timestamp with time zone,
    released_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.stock_reservations (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    sku_id uuid NOT NULL,
    user_id uuid NOT NULL,
    cart_token_hash text,
    qty integer NOT NULL,
    payment_intent_id text,
    expires_at timestamp with time zone NOT NULL,
    consumed_at timestamp with time zone,
    released_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.stock_watchlist (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    deal_id uuid NOT NULL,
    sku_id uuid,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    notified_at timestamp with time zone,
    expires_at timestamp with time zone DEFAULT (now() + '30 days'::interval) NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.stripe_webhook_events (
    event_id text NOT NULL,
    event_type text NOT NULL,
    received_at timestamp with time zone DEFAULT now() NOT NULL,
    processed_at timestamp with time zone,
    claimed_at timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE public.support_attachments (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    parent_type public.support_parent_type NOT NULL,
    parent_id uuid NOT NULL,
    message_id uuid,
    cf_image_id text NOT NULL,
    variants jsonb NOT NULL,
    mime text NOT NULL,
    size_bytes integer NOT NULL,
    uploader_id uuid NOT NULL,
    uploader_role public.support_opener_role NOT NULL,
    abuse_status public.support_attachment_abuse_status DEFAULT 'pending'::public.support_attachment_abuse_status NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.support_kb_article_translations (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    article_id uuid NOT NULL,
    locale text NOT NULL,
    slug text NOT NULL,
    title text NOT NULL,
    body_md text NOT NULL,
    slug_manual_override boolean DEFAULT true NOT NULL,
    title_manual_override boolean DEFAULT true NOT NULL,
    body_md_manual_override boolean DEFAULT true NOT NULL,
    model_id text,
    status text DEFAULT 'OK'::text NOT NULL,
    translated_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.support_kb_articles (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    slug text NOT NULL,
    category public.support_kb_category NOT NULL,
    tags text[] DEFAULT '{}'::text[] NOT NULL,
    visibility public.support_kb_visibility DEFAULT 'public'::public.support_kb_visibility NOT NULL,
    is_active boolean DEFAULT true NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_by uuid,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.support_kb_embeddings (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    article_id uuid NOT NULL,
    chunk_index integer DEFAULT 0 NOT NULL,
    chunk_text text NOT NULL,
    embedding public.vector(1024) NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.support_messages (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    parent_type public.support_parent_type NOT NULL,
    parent_id uuid NOT NULL,
    author_type public.support_message_author_type NOT NULL,
    author_id uuid,
    visibility public.support_message_visibility DEFAULT 'public'::public.support_message_visibility NOT NULL,
    body_encrypted text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    edited_at timestamp with time zone,
    deleted_at timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE public.support_state_transitions (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    parent_type public.support_parent_type NOT NULL,
    parent_id uuid NOT NULL,
    from_state text NOT NULL,
    to_state text NOT NULL,
    actor_type text NOT NULL,
    actor_id uuid,
    reason text,
    metadata jsonb DEFAULT '{}'::jsonb NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.support_tickets (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    opener_id uuid NOT NULL,
    opener_role public.support_opener_role NOT NULL,
    category public.support_ticket_category NOT NULL,
    status public.support_ticket_status DEFAULT 'open'::public.support_ticket_status NOT NULL,
    priority public.support_priority DEFAULT 'normal'::public.support_priority NOT NULL,
    ai_confidence numeric(4,3),
    assigned_agent_id uuid,
    related_order_id uuid,
    reopen_count integer DEFAULT 0 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    resolved_at timestamp with time zone,
    closed_at timestamp with time zone,
    metadata jsonb DEFAULT '{}'::jsonb NOT NULL,
    agent_definition_id uuid,
    related_order_line_id uuid
);
--> statement-breakpoint
CREATE TABLE public.sync_cursors (
    key text NOT NULL,
    value text NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.system_config (
    key text NOT NULL,
    value text NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_by text
);
--> statement-breakpoint
CREATE TABLE public.system_health_findings (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    kind text NOT NULL,
    entity_type text NOT NULL,
    entity_id text NOT NULL,
    detail text,
    first_seen_at timestamp with time zone DEFAULT now() NOT NULL,
    last_seen_at timestamp with time zone DEFAULT now() NOT NULL,
    resolved_at timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE public.tag_translations (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    tag_id uuid NOT NULL,
    locale text NOT NULL,
    name text NOT NULL,
    name_source_hash text NOT NULL,
    name_manual_override boolean DEFAULT false NOT NULL,
    model_id text,
    status text DEFAULT 'OK'::text NOT NULL,
    translated_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.tax_documents (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    order_line_id uuid NOT NULL,
    side text NOT NULL,
    provider_tax_doc_id text NOT NULL,
    idempotency_key text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.transaction_cases (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    order_line_id uuid NOT NULL,
    customer_id uuid NOT NULL,
    vendor_id uuid NOT NULL,
    category public.support_case_category NOT NULL,
    status public.support_case_status DEFAULT 'opened'::public.support_case_status NOT NULL,
    escape_used boolean DEFAULT false NOT NULL,
    vendor_window_hours integer NOT NULL,
    vendor_window_expires_at timestamp with time zone,
    sla_ai_due_at timestamp with time zone,
    sla_human_due_at timestamp with time zone,
    autoclose_at timestamp with time zone,
    ai_confidence numeric(4,3),
    assigned_agent_id uuid,
    reopen_count integer DEFAULT 0 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    resolved_at timestamp with time zone,
    closed_at timestamp with time zone,
    metadata jsonb DEFAULT '{}'::jsonb NOT NULL,
    agent_definition_id uuid
);
--> statement-breakpoint
CREATE TABLE public.translation_glossary (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    term text NOT NULL,
    mode text NOT NULL,
    locale text,
    replacement text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.translation_jobs (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    deal_id uuid NOT NULL,
    target_locale text NOT NULL,
    status text DEFAULT 'PENDING'::text NOT NULL,
    attempt integer DEFAULT 0 NOT NULL,
    last_error text,
    cost_usd numeric(10,6),
    input_tokens integer,
    output_tokens integer,
    force_fresh boolean DEFAULT false NOT NULL,
    scheduled_at timestamp with time zone,
    started_at timestamp with time zone,
    finished_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.translation_memory (
    source_hash text NOT NULL,
    source_locale text NOT NULL,
    target_locale text NOT NULL,
    source_text text NOT NULL,
    translated_text text NOT NULL,
    model_id text NOT NULL,
    quality_score integer,
    usage_count integer DEFAULT 1 NOT NULL,
    last_used_at timestamp with time zone DEFAULT now() NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.user_gallery_images (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    vendor_id uuid NOT NULL,
    order_line_id uuid,
    url text NOT NULL,
    caption text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    approval_status public.image_approval_status DEFAULT 'APPROVED'::public.image_approval_status NOT NULL,
    reject_reason_code text
);
--> statement-breakpoint
CREATE TABLE public.user_recently_viewed (
    user_id uuid NOT NULL,
    deal_id uuid NOT NULL,
    viewed_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.users (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    phone text,
    phone_index text,
    email text,
    email_index text,
    avatar_type public.avatar_type DEFAULT 'ICON'::public.avatar_type NOT NULL,
    avatar_value text DEFAULT ''::text NOT NULL,
    email_2fa_enabled boolean DEFAULT true NOT NULL,
    purchase_count integer DEFAULT 0 NOT NULL,
    preferences_profile jsonb,
    account_state public.account_state_user DEFAULT 'ACTIVE'::public.account_state_user NOT NULL,
    is_admin boolean DEFAULT false NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    deletion_requested_at timestamp with time zone,
    password_hash text,
    email_verified_at timestamp with time zone,
    avatar_approval_status public.image_approval_status DEFAULT 'APPROVED'::public.image_approval_status NOT NULL,
    pending_avatar_value text,
    avatar_reject_reason_code text,
    city text,
    city_code text,
    birth_month integer,
    birth_day integer,
    onboarding_completed_at timestamp with time zone,
    mh_version integer DEFAULT 1 NOT NULL,
    preferred_city_code text,
    session_version integer DEFAULT 0 NOT NULL,
    display_name text,
    stripe_customer_id text,
    default_payment_method_id text,
    preferences jsonb DEFAULT '{}'::jsonb NOT NULL,
    notif_prefs jsonb DEFAULT '{}'::jsonb NOT NULL,
    email_canonical_index text,
    phone_hint text,
    CONSTRAINT users_birth_day_range_check CHECK (((birth_day IS NULL) OR ((birth_day >= 1) AND (birth_day <= 31)))),
    CONSTRAINT users_birth_month_range_check CHECK (((birth_month IS NULL) OR ((birth_month >= 1) AND (birth_month <= 12)))),
    CONSTRAINT users_email_cols_consistent CHECK ((((email IS NULL) AND (email_index IS NULL)) OR ((email IS NOT NULL) AND (email_index IS NOT NULL)))),
    CONSTRAINT users_phone_cols_consistent CHECK ((((phone IS NULL) AND (phone_index IS NULL) AND (phone_hint IS NULL)) OR ((phone IS NOT NULL) AND (phone_index IS NOT NULL) AND (phone_hint IS NOT NULL))))
);
--> statement-breakpoint
CREATE TABLE public.vendor_addresses (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    vendor_id uuid NOT NULL,
    label text NOT NULL,
    full_address text NOT NULL,
    lat numeric(10,7) NOT NULL,
    lng numeric(10,7) NOT NULL,
    city text DEFAULT ''::text NOT NULL,
    is_public boolean DEFAULT false NOT NULL,
    city_code text DEFAULT ''::text NOT NULL,
    street_code text DEFAULT ''::text NOT NULL,
    street_name text DEFAULT ''::text NOT NULL,
    house_number text DEFAULT ''::text NOT NULL,
    apt text DEFAULT ''::text NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.vendor_business_types (
    vendor_id uuid NOT NULL,
    business_type_id uuid NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.vendor_gallery_images (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    vendor_id uuid NOT NULL,
    url text NOT NULL,
    caption text,
    sort_order integer DEFAULT 0 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    approval_status public.image_approval_status DEFAULT 'APPROVED'::public.image_approval_status NOT NULL,
    reject_reason_code text
);
--> statement-breakpoint
CREATE TABLE public.vendor_invoice_settings (
    vendor_id uuid NOT NULL,
    provider public.invoice_provider DEFAULT 'self_handled'::public.invoice_provider NOT NULL,
    credentials_ciphertext text,
    credentials_iv text,
    credentials_tag text,
    settings_json jsonb DEFAULT '{}'::jsonb NOT NULL,
    last_test_at timestamp with time zone,
    last_test_ok boolean,
    last_test_error text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.vendor_payout_releases (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    order_line_id uuid NOT NULL,
    vendor_id uuid NOT NULL,
    vendor_acct_id text NOT NULL,
    payment_intent_id text NOT NULL,
    charge_id text NOT NULL,
    transfer_id text NOT NULL,
    net_amount_agorot integer NOT NULL,
    application_fee_agorot integer NOT NULL,
    status public.vendor_payout_release_status DEFAULT 'held'::public.vendor_payout_release_status NOT NULL,
    trigger_reason text NOT NULL,
    held_at timestamp with time zone DEFAULT now() NOT NULL,
    release_at timestamp with time zone NOT NULL,
    enqueued_at timestamp with time zone,
    claimed_at timestamp with time zone,
    released_at timestamp with time zone,
    cancelled_at timestamp with time zone,
    payout_id text,
    payout_idempotency_key text,
    payout_idempotency_key_created_at timestamp with time zone,
    payout_batch_key text,
    refund_id text,
    claim_attempts integer DEFAULT 0 NOT NULL,
    last_error text,
    metadata jsonb DEFAULT '{}'::jsonb NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.vendor_recommendations (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    vendor_id uuid NOT NULL,
    kind text NOT NULL,
    payload jsonb NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    dismissed_at timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE public.vendor_split (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    order_id uuid NOT NULL,
    vendor_id text,
    amount bigint NOT NULL,
    funder text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.vendor_translations (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    vendor_id uuid NOT NULL,
    locale text NOT NULL,
    display_name text,
    display_name_source_hash text,
    display_name_manual_override boolean DEFAULT false NOT NULL,
    description text,
    description_source_hash text,
    description_manual_override boolean DEFAULT false NOT NULL,
    model_id text,
    status text DEFAULT 'OK'::text NOT NULL,
    translated_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE public.vendors (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    owner_user_id uuid NOT NULL,
    business_name text NOT NULL,
    display_name text NOT NULL,
    description text DEFAULT ''::text NOT NULL,
    phone text NOT NULL,
    email text NOT NULL,
    logo_url text,
    hero_image_url text,
    tier public.vendor_tier DEFAULT 'NEW'::public.vendor_tier NOT NULL,
    account_state public.account_state_vendor DEFAULT 'PENDING_FIRST_APPROVAL'::public.account_state_vendor NOT NULL,
    total_sales integer DEFAULT 0 NOT NULL,
    total_revenue numeric(12,2) DEFAULT '0'::numeric NOT NULL,
    reviews_score numeric(3,2) DEFAULT '0'::numeric NOT NULL,
    reviews_count integer DEFAULT 0 NOT NULL,
    removed_reviews_count integer DEFAULT 0 NOT NULL,
    unresolved_voucher_complaints integer DEFAULT 0 NOT NULL,
    first_voucher_complaint_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    website text,
    preferences_profile jsonb DEFAULT '{}'::jsonb,
    deal_violations_count integer DEFAULT 0 NOT NULL,
    hero_image_r2_key text,
    hero_focal_x real DEFAULT 0.5 NOT NULL,
    hero_focal_y real DEFAULT 0.5 NOT NULL,
    hero_image_approval_status public.image_approval_status DEFAULT 'PENDING'::public.image_approval_status NOT NULL,
    hero_pending_image_url text,
    hero_pending_image_r2_key text,
    hero_image_reject_reason_code text,
    logo_approval_status public.image_approval_status DEFAULT 'APPROVED'::public.image_approval_status NOT NULL,
    pending_logo_url text,
    pending_logo_r2_key text,
    logo_reject_reason_code text,
    flag_reason text,
    reject_reason text,
    llm_decision text,
    mock_scenario text,
    stripe_account_id text,
    stripe_charges_enabled boolean DEFAULT false NOT NULL,
    stripe_payouts_enabled boolean DEFAULT false NOT NULL,
    stripe_onboarding_state text DEFAULT 'not_started'::text NOT NULL,
    stripe_details_submitted boolean DEFAULT false NOT NULL,
    stripe_requirements_currently_due jsonb,
    self_pickup boolean DEFAULT false NOT NULL,
    return_address jsonb,
    restocking_fee_pct numeric(4,3) DEFAULT 0 NOT NULL,
    pickup_address text
);
--> statement-breakpoint
CREATE TABLE public.voucher (
    id uuid NOT NULL,
    order_id uuid NOT NULL,
    line_id text NOT NULL,
    unit_index integer NOT NULL,
    vendor_id text,
    state text DEFAULT 'UNREDEEMED'::text NOT NULL,
    expires_at timestamp with time zone,
    redeemed_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT voucher_state_chk CHECK ((state = ANY (ARRAY['UNREDEEMED'::text, 'REDEEMED'::text, 'EXPIRED'::text, 'CANCELLED'::text]))),
    CONSTRAINT voucher_unit_index_chk CHECK ((unit_index >= 0))
);
--> statement-breakpoint
CREATE TABLE public.wallet_balances (
    user_id uuid NOT NULL,
    balance_agorot bigint DEFAULT 0 NOT NULL,
    lifetime_earned_agorot bigint DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    pending_agorot bigint DEFAULT 0 NOT NULL,
    matured_agorot bigint DEFAULT 0 NOT NULL,
    withdrawable_agorot bigint DEFAULT 0 NOT NULL,
    CONSTRAINT wallet_balances_pending_agorot_nonneg CHECK ((pending_agorot >= 0))
);
--> statement-breakpoint
CREATE TABLE public.wallet_vesting (
    owner_id text NOT NULL,
    pending_minor bigint DEFAULT 0 NOT NULL,
    matured_minor bigint DEFAULT 0 NOT NULL,
    withdrawable_minor bigint DEFAULT 0 NOT NULL,
    lifetime_earned_minor bigint DEFAULT 0 NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    carried_debt_minor bigint DEFAULT 0 NOT NULL,
    CONSTRAINT wallet_vesting_carried_debt_minor_nonneg CHECK ((carried_debt_minor >= 0)),
    CONSTRAINT wallet_vesting_matured_minor_nonneg CHECK ((matured_minor >= 0)),
    CONSTRAINT wallet_vesting_pending_minor_nonneg CHECK ((pending_minor >= 0)),
    CONSTRAINT wallet_vesting_withdrawable_minor_nonneg CHECK ((withdrawable_minor >= 0))
);
--> statement-breakpoint
CREATE TABLE public.wishlist_items (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    deal_id uuid NOT NULL,
    notify boolean DEFAULT false NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE ONLY public.access_grant
    ADD CONSTRAINT access_grant_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.address_zip_cache_misses
    ADD CONSTRAINT address_zip_cache_misses_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.addresses
    ADD CONSTRAINT addresses_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.admin_actions
    ADD CONSTRAINT admin_actions_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.admin_assignments
    ADD CONSTRAINT admin_assignments_pkey PRIMARY KEY (scope, entity_id);
--> statement-breakpoint
ALTER TABLE ONLY public.affiliate_admin_actions
    ADD CONSTRAINT affiliate_admin_actions_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.affiliate_enrollments
    ADD CONSTRAINT affiliate_enrollments_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.affiliate_enrollments
    ADD CONSTRAINT affiliate_enrollments_stripe_account_id_key UNIQUE (stripe_account_id);
--> statement-breakpoint
ALTER TABLE ONLY public.affiliate_enrollments
    ADD CONSTRAINT affiliate_enrollments_user_id_key UNIQUE (user_id);
--> statement-breakpoint
ALTER TABLE ONLY public.affiliate_entries
    ADD CONSTRAINT affiliate_entries_pkey PRIMARY KEY (entry_id);
--> statement-breakpoint
ALTER TABLE ONLY public.affiliate_payouts
    ADD CONSTRAINT affiliate_payouts_idempotency_key_key UNIQUE (idempotency_key);
--> statement-breakpoint
ALTER TABLE ONLY public.affiliate_payouts
    ADD CONSTRAINT affiliate_payouts_ledger_entry_id_key UNIQUE (ledger_entry_id);
--> statement-breakpoint
ALTER TABLE ONLY public.affiliate_payouts
    ADD CONSTRAINT affiliate_payouts_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.affiliate_payouts
    ADD CONSTRAINT affiliate_payouts_stripe_payout_id_key UNIQUE (stripe_payout_id);
--> statement-breakpoint
ALTER TABLE ONLY public.affiliate_payouts
    ADD CONSTRAINT affiliate_payouts_stripe_transfer_id_key UNIQUE (stripe_transfer_id);
--> statement-breakpoint
ALTER TABLE ONLY public.agent_definition_versions
    ADD CONSTRAINT agent_definition_versions_definition_id_version_key UNIQUE (definition_id, version);
--> statement-breakpoint
ALTER TABLE ONLY public.agent_definition_versions
    ADD CONSTRAINT agent_definition_versions_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.agent_definitions
    ADD CONSTRAINT agent_definitions_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.agent_definitions
    ADD CONSTRAINT agent_definitions_slug_key UNIQUE (slug);
--> statement-breakpoint
ALTER TABLE ONLY public.ai_interventions
    ADD CONSTRAINT ai_interventions_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.alert_channels
    ADD CONSTRAINT alert_channels_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.business_hours
    ADD CONSTRAINT business_hours_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.business_hours
    ADD CONSTRAINT business_hours_vendor_id_unique UNIQUE (vendor_id);
--> statement-breakpoint
ALTER TABLE ONLY public.business_types
    ADD CONSTRAINT business_types_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.carrier_webhook_event
    ADD CONSTRAINT carrier_webhook_event_pkey PRIMARY KEY (event_id);
--> statement-breakpoint
ALTER TABLE ONLY public.carrier_webhook_events
    ADD CONSTRAINT carrier_webhook_events_pkey PRIMARY KEY (carrier, event_id);
--> statement-breakpoint
ALTER TABLE ONLY public.cart_checkout_captures
    ADD CONSTRAINT cart_checkout_captures_checkout_order_uniq UNIQUE (checkout_key, order_id);
--> statement-breakpoint
ALTER TABLE ONLY public.cart_checkout_captures
    ADD CONSTRAINT cart_checkout_captures_hold_uniq UNIQUE (provider_hold_id);
--> statement-breakpoint
ALTER TABLE ONLY public.cart_checkout_captures
    ADD CONSTRAINT cart_checkout_captures_payment_uniq UNIQUE (provider_payment_id);
--> statement-breakpoint
ALTER TABLE ONLY public.cart_checkout_captures
    ADD CONSTRAINT cart_checkout_captures_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.cart_checkout_compensations
    ADD CONSTRAINT cart_checkout_compensations_checkout_order_uniq UNIQUE (checkout_key, order_id);
--> statement-breakpoint
ALTER TABLE ONLY public.cart_checkout_compensations
    ADD CONSTRAINT cart_checkout_compensations_payment_uniq UNIQUE (provider_payment_id);
--> statement-breakpoint
ALTER TABLE ONLY public.cart_checkout_compensations
    ADD CONSTRAINT cart_checkout_compensations_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.cart_items
    ADD CONSTRAINT cart_items_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.cart_items
    ADD CONSTRAINT cart_items_user_id_deal_sku_id_unique UNIQUE (user_id, deal_sku_id);
--> statement-breakpoint
ALTER TABLE ONLY public.cart_line
    ADD CONSTRAINT cart_line_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.cart
    ADD CONSTRAINT cart_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.case_offers
    ADD CONSTRAINT case_offers_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.case_resolutions
    ADD CONSTRAINT case_resolutions_case_id_unique UNIQUE (case_id);
--> statement-breakpoint
ALTER TABLE ONLY public.case_resolutions
    ADD CONSTRAINT case_resolutions_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.category_translations
    ADD CONSTRAINT category_translations_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.chat_messages
    ADD CONSTRAINT chat_messages_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.chat_thread_participants
    ADD CONSTRAINT chat_thread_participants_pkey PRIMARY KEY (thread_id, user_id);
--> statement-breakpoint
ALTER TABLE ONLY public.chat_threads
    ADD CONSTRAINT chat_threads_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.club_memberships
    ADD CONSTRAINT club_memberships_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.club_memberships
    ADD CONSTRAINT club_user_vendor_unique UNIQUE (user_id, vendor_id);
--> statement-breakpoint
ALTER TABLE ONLY public.command_records
    ADD CONSTRAINT command_records_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.credit_ledger
    ADD CONSTRAINT credit_ledger_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.cron_cursors
    ADD CONSTRAINT cron_cursors_pkey PRIMARY KEY (job_key);
--> statement-breakpoint
ALTER TABLE ONLY public.cron_runs
    ADD CONSTRAINT cron_runs_pkey PRIMARY KEY (cron_key);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_annotations
    ADD CONSTRAINT deal_annotations_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_categories
    ADD CONSTRAINT deal_categories_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_drafts
    ADD CONSTRAINT deal_drafts_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_images
    ADD CONSTRAINT deal_images_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_item_config
    ADD CONSTRAINT deal_item_config_pkey PRIMARY KEY (deal_id);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_skus
    ADD CONSTRAINT deal_skus_deal_combo_uniq UNIQUE (deal_id, option_ids_hash);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_skus
    ADD CONSTRAINT deal_skus_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_slug_redirects
    ADD CONSTRAINT deal_slug_redirects_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_tag_assignments
    ADD CONSTRAINT deal_tag_assignments_deal_id_tag_id_pk PRIMARY KEY (deal_id, tag_id);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_tags
    ADD CONSTRAINT deal_tags_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_translations
    ADD CONSTRAINT deal_translations_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_variant_axes
    ADD CONSTRAINT deal_variant_axes_deal_order_uniq UNIQUE (deal_id, axis_order);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_variant_axes
    ADD CONSTRAINT deal_variant_axes_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_variant_options
    ADD CONSTRAINT deal_variant_options_axis_code_uniq UNIQUE (axis_id, value_code);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_variant_options
    ADD CONSTRAINT deal_variant_options_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.deals
    ADD CONSTRAINT deals_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.dlq_events
    ADD CONSTRAINT dlq_events_pkey PRIMARY KEY (queue_key);
--> statement-breakpoint
ALTER TABLE ONLY public.e2e_factory_commands
    ADD CONSTRAINT e2e_factory_commands_pk PRIMARY KEY (run_id, request_id);
--> statement-breakpoint
ALTER TABLE ONLY public.e2e_factory_entities
    ADD CONSTRAINT e2e_factory_entities_pk PRIMARY KEY (run_id, kind, entity_id);
--> statement-breakpoint
ALTER TABLE ONLY public.e2e_factory_runs
    ADD CONSTRAINT e2e_factory_runs_pkey PRIMARY KEY (run_id);
--> statement-breakpoint
ALTER TABLE ONLY public.email_verifications
    ADD CONSTRAINT email_verifications_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.email_verifications
    ADD CONSTRAINT email_verifications_token_hash_unique UNIQUE (token_hash);
--> statement-breakpoint
ALTER TABLE ONLY public.favorite_vendors
    ADD CONSTRAINT favorite_vendors_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.favorite_vendors
    ADD CONSTRAINT favorite_vendors_user_vendor_unique UNIQUE (user_id, vendor_id);
--> statement-breakpoint
ALTER TABLE ONLY public.fraud_events
    ADD CONSTRAINT fraud_events_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.group_deals
    ADD CONSTRAINT group_deals_deal_id_unique UNIQUE (deal_id);
--> statement-breakpoint
ALTER TABLE ONLY public.group_deals
    ADD CONSTRAINT group_deals_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.group_reservations
    ADD CONSTRAINT group_reservations_idempotency_key_unique UNIQUE (idempotency_key);
--> statement-breakpoint
ALTER TABLE ONLY public.group_reservations
    ADD CONSTRAINT group_reservations_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.group_tiers
    ADD CONSTRAINT group_tiers_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.group_waitlist
    ADD CONSTRAINT group_waitlist_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.image_uploads
    ADD CONSTRAINT image_uploads_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.inventory_item
    ADD CONSTRAINT inventory_item_pkey PRIMARY KEY (sku_id);
--> statement-breakpoint
ALTER TABLE ONLY public.invoices
    ADD CONSTRAINT invoices_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.kyc_verifications
    ADD CONSTRAINT kyc_verifications_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.kyc_verifications
    ADD CONSTRAINT kyc_verifications_user_id_unique UNIQUE (user_id);
--> statement-breakpoint
ALTER TABLE ONLY public.languages
    ADD CONSTRAINT languages_pkey PRIMARY KEY (code);
--> statement-breakpoint
ALTER TABLE ONLY public.ledger_entries
    ADD CONSTRAINT ledger_entries_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.ledger_entry_vesting
    ADD CONSTRAINT ledger_entry_vesting_pkey PRIMARY KEY (entry_id);
--> statement-breakpoint
ALTER TABLE ONLY public.ledger_reconciliation_repairs
    ADD CONSTRAINT ledger_reconciliation_repairs_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.live_notifications
    ADD CONSTRAINT live_notifications_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.llm_jobs
    ADD CONSTRAINT llm_jobs_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.llm_model_cache
    ADD CONSTRAINT llm_model_cache_pkey PRIMARY KEY (provider_id);
--> statement-breakpoint
ALTER TABLE ONLY public.llm_provider_configs
    ADD CONSTRAINT llm_provider_configs_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.llm_providers
    ADD CONSTRAINT llm_providers_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.llm_providers
    ADD CONSTRAINT llm_providers_slug_key UNIQUE (slug);
--> statement-breakpoint
ALTER TABLE ONLY public.llm_queues
    ADD CONSTRAINT llm_queues_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.loyalty_memberships
    ADD CONSTRAINT loyalty_memberships_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.loyalty_memberships
    ADD CONSTRAINT loyalty_user_vendor_unique UNIQUE (user_id, vendor_id);
--> statement-breakpoint
ALTER TABLE ONLY public.magic_links
    ADD CONSTRAINT magic_links_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.magic_links
    ADD CONSTRAINT magic_links_token_hash_unique UNIQUE (token_hash);
--> statement-breakpoint
ALTER TABLE ONLY public.marketing_trigger_state
    ADD CONSTRAINT marketing_trigger_state_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.marketing_trigger_state
    ADD CONSTRAINT marketing_trigger_state_user_id_trigger_key_key UNIQUE (user_id, trigger_key);
--> statement-breakpoint
ALTER TABLE ONLY public.mock_payment_events
    ADD CONSTRAINT mock_payment_events_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.monitor_checks
    ADD CONSTRAINT monitor_checks_pkey PRIMARY KEY (key);
--> statement-breakpoint
ALTER TABLE ONLY public.notifications
    ADD CONSTRAINT notifications_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.ops_alert_state
    ADD CONSTRAINT ops_alert_state_pkey PRIMARY KEY (alert_key);
--> statement-breakpoint
ALTER TABLE ONLY public.order_guest_contact
    ADD CONSTRAINT order_guest_contact_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.order_line
    ADD CONSTRAINT order_line_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.order_line_voucher_ext
    ADD CONSTRAINT order_line_voucher_ext_pkey PRIMARY KEY (voucher_id);
--> statement-breakpoint
ALTER TABLE ONLY public.order_payment_fees
    ADD CONSTRAINT order_payment_fees_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public."order"
    ADD CONSTRAINT order_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.order_share_attribution
    ADD CONSTRAINT order_share_attribution_order_id_key UNIQUE (order_id);
--> statement-breakpoint
ALTER TABLE ONLY public.order_share_attribution
    ADD CONSTRAINT order_share_attribution_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.order_step
    ADD CONSTRAINT order_step_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.outbox
    ADD CONSTRAINT outbox_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.page_layout_pointers
    ADD CONSTRAINT page_layout_pointers_pkey PRIMARY KEY (page);
--> statement-breakpoint
ALTER TABLE ONLY public.page_layouts
    ADD CONSTRAINT page_layouts_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.payment_expected_contracts
    ADD CONSTRAINT payment_expected_contract_purchase_uniq UNIQUE (purchase_id);
--> statement-breakpoint
ALTER TABLE ONLY public.payment_expected_contracts
    ADD CONSTRAINT payment_expected_contracts_effect_key_key UNIQUE (effect_key);
--> statement-breakpoint
ALTER TABLE ONLY public.payment_expected_contracts
    ADD CONSTRAINT payment_expected_contracts_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.payment_finalize_checkpoints
    ADD CONSTRAINT payment_finalize_checkpoint_effect_uniq UNIQUE (purchase_id, effect_key);
--> statement-breakpoint
ALTER TABLE ONLY public.payment_finalize_checkpoints
    ADD CONSTRAINT payment_finalize_checkpoints_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.payment_methods
    ADD CONSTRAINT payment_methods_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.payout_audit_log
    ADD CONSTRAINT payout_audit_log_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.payouts
    ADD CONSTRAINT payouts_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.payouts
    ADD CONSTRAINT payouts_vendor_period_uniq UNIQUE (vendor_id, period_month);
--> statement-breakpoint
ALTER TABLE ONLY public.personal_deal_requests
    ADD CONSTRAINT personal_deal_requests_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.platform_image_limits
    ADD CONSTRAINT platform_image_limits_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.promo_allowlist
    ADD CONSTRAINT promo_allowlist_pkey PRIMARY KEY (promo_id, user_id);
--> statement-breakpoint
ALTER TABLE ONLY public.promo
    ADD CONSTRAINT promo_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.promo_redemptions
    ADD CONSTRAINT promo_redemptions_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.promo_redemptions
    ADD CONSTRAINT promo_redemptions_purchase_id_promo_code_id_key UNIQUE (order_line_id, promo_code_id);
--> statement-breakpoint
ALTER TABLE ONLY public.promo_reservations
    ADD CONSTRAINT promo_reservations_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.promo_user_usage
    ADD CONSTRAINT promo_user_usage_pkey PRIMARY KEY (promo_id, user_id);
--> statement-breakpoint
ALTER TABLE ONLY public.purchase_messages
    ADD CONSTRAINT purchase_messages_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.purchases_archived_pre_wipe
    ADD CONSTRAINT purchases_idempotency_key_unique UNIQUE (idempotency_key);
--> statement-breakpoint
ALTER TABLE ONLY public.purchases_archived_pre_wipe
    ADD CONSTRAINT purchases_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.push_subscriptions
    ADD CONSTRAINT push_subscriptions_endpoint_unique UNIQUE (endpoint);
--> statement-breakpoint
ALTER TABLE ONLY public.push_subscriptions
    ADD CONSTRAINT push_subscriptions_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.rate_limit_buckets
    ADD CONSTRAINT rate_limit_buckets_pkey PRIMARY KEY (key);
--> statement-breakpoint
ALTER TABLE ONLY public.referral_link_stats_daily
    ADD CONSTRAINT referral_link_stats_daily_pkey PRIMARY KEY (link_id, day);
--> statement-breakpoint
ALTER TABLE ONLY public.referral_links
    ADD CONSTRAINT referral_links_code_unique UNIQUE (code);
--> statement-breakpoint
ALTER TABLE ONLY public.referral_links
    ADD CONSTRAINT referral_links_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.referral_settings
    ADD CONSTRAINT referral_settings_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.referrals
    ADD CONSTRAINT referrals_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.referrals
    ADD CONSTRAINT referrals_referee_user_id_unique UNIQUE (referee_user_id);
--> statement-breakpoint
ALTER TABLE ONLY public.refund_intent_line_ext
    ADD CONSTRAINT refund_intent_line_ext_pkey PRIMARY KEY (refund_intent_id);
--> statement-breakpoint
ALTER TABLE ONLY public.refund_intent
    ADD CONSTRAINT refund_intent_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.refunds
    ADD CONSTRAINT refunds_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.refunds
    ADD CONSTRAINT refunds_purchase_id_seq_key UNIQUE (purchase_id, seq);
--> statement-breakpoint
ALTER TABLE ONLY public.report_tickets
    ADD CONSTRAINT report_tickets_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.return_events
    ADD CONSTRAINT return_events_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.returns
    ADD CONSTRAINT returns_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.review
    ADD CONSTRAINT review_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.review_removal_queue
    ADD CONSTRAINT review_removal_queue_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.review_removal_queue
    ADD CONSTRAINT review_removal_queue_review_id_unique UNIQUE (review_id);
--> statement-breakpoint
ALTER TABLE ONLY public.review_translations
    ADD CONSTRAINT review_translations_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.reviews
    ADD CONSTRAINT reviews_order_line_id_unique UNIQUE (order_line_id);
--> statement-breakpoint
ALTER TABLE ONLY public.reviews
    ADD CONSTRAINT reviews_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.sessions
    ADD CONSTRAINT sessions_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.share_audience_daily
    ADD CONSTRAINT share_audience_daily_pkey PRIMARY KEY (day, device, country);
--> statement-breakpoint
ALTER TABLE ONLY public.share_link_stats_daily
    ADD CONSTRAINT share_link_stats_daily_pkey PRIMARY KEY (link_id, day);
--> statement-breakpoint
ALTER TABLE ONLY public.share_links
    ADD CONSTRAINT share_links_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.share_links
    ADD CONSTRAINT share_links_slug_key UNIQUE (slug);
--> statement-breakpoint
ALTER TABLE ONLY public.shipment_events
    ADD CONSTRAINT shipment_events_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.shipment
    ADD CONSTRAINT shipment_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.shipment_purchases
    ADD CONSTRAINT shipment_purchases_pkey PRIMARY KEY (shipment_id, order_line_id);
--> statement-breakpoint
ALTER TABLE ONLY public.shipments
    ADD CONSTRAINT shipments_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.shipping_addresses
    ADD CONSTRAINT shipping_addresses_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.sku_qty_tiers
    ADD CONSTRAINT sku_qty_tiers_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.slug_history
    ADD CONSTRAINT slug_history_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.standing_offers
    ADD CONSTRAINT standing_offers_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.stock_reservation
    ADD CONSTRAINT stock_reservation_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.stock_reservations
    ADD CONSTRAINT stock_reservations_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.stock_watchlist
    ADD CONSTRAINT stock_watchlist_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.stripe_webhook_events
    ADD CONSTRAINT stripe_webhook_events_pkey PRIMARY KEY (event_id);
--> statement-breakpoint
ALTER TABLE ONLY public.support_attachments
    ADD CONSTRAINT support_attachments_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.support_kb_article_translations
    ADD CONSTRAINT support_kb_article_translations_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.support_kb_articles
    ADD CONSTRAINT support_kb_articles_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.support_kb_embeddings
    ADD CONSTRAINT support_kb_embeddings_article_id_chunk_index_key UNIQUE (article_id, chunk_index);
--> statement-breakpoint
ALTER TABLE ONLY public.support_kb_embeddings
    ADD CONSTRAINT support_kb_embeddings_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.support_messages
    ADD CONSTRAINT support_messages_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.support_state_transitions
    ADD CONSTRAINT support_state_transitions_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.support_tickets
    ADD CONSTRAINT support_tickets_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.sync_cursors
    ADD CONSTRAINT sync_cursors_pkey PRIMARY KEY (key);
--> statement-breakpoint
ALTER TABLE ONLY public.system_config
    ADD CONSTRAINT system_config_pkey PRIMARY KEY (key);
--> statement-breakpoint
ALTER TABLE ONLY public.system_health_findings
    ADD CONSTRAINT system_health_findings_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.tag_translations
    ADD CONSTRAINT tag_translations_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.tax_documents
    ADD CONSTRAINT tax_documents_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.transaction_cases
    ADD CONSTRAINT transaction_cases_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.translation_glossary
    ADD CONSTRAINT translation_glossary_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.translation_jobs
    ADD CONSTRAINT translation_jobs_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.translation_memory
    ADD CONSTRAINT translation_memory_source_hash_source_locale_target_locale_pk PRIMARY KEY (source_hash, source_locale, target_locale);
--> statement-breakpoint
ALTER TABLE ONLY public.user_gallery_images
    ADD CONSTRAINT user_gallery_images_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.user_recently_viewed
    ADD CONSTRAINT user_recently_viewed_pkey PRIMARY KEY (user_id, deal_id);
--> statement-breakpoint
ALTER TABLE ONLY public.users
    ADD CONSTRAINT users_email_index_unique UNIQUE (email_index);
--> statement-breakpoint
ALTER TABLE ONLY public.users
    ADD CONSTRAINT users_phone_index_unique UNIQUE (phone_index);
--> statement-breakpoint
ALTER TABLE ONLY public.users
    ADD CONSTRAINT users_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.vendor_addresses
    ADD CONSTRAINT vendor_addresses_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.vendor_business_types
    ADD CONSTRAINT vendor_business_types_pk PRIMARY KEY (vendor_id, business_type_id);
--> statement-breakpoint
ALTER TABLE ONLY public.vendor_gallery_images
    ADD CONSTRAINT vendor_gallery_images_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.vendor_invoice_settings
    ADD CONSTRAINT vendor_invoice_settings_pkey PRIMARY KEY (vendor_id);
--> statement-breakpoint
ALTER TABLE ONLY public.vendor_payout_releases
    ADD CONSTRAINT vendor_payout_releases_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.vendor_recommendations
    ADD CONSTRAINT vendor_recommendations_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.vendor_split
    ADD CONSTRAINT vendor_split_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.vendor_translations
    ADD CONSTRAINT vendor_translations_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.vendors
    ADD CONSTRAINT vendors_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.voucher
    ADD CONSTRAINT voucher_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.wallet_balances
    ADD CONSTRAINT wallet_balances_pkey PRIMARY KEY (user_id);
--> statement-breakpoint
ALTER TABLE ONLY public.wallet_vesting
    ADD CONSTRAINT wallet_vesting_pkey PRIMARY KEY (owner_id);
--> statement-breakpoint
ALTER TABLE ONLY public.wishlist_items
    ADD CONSTRAINT wishlist_items_pkey PRIMARY KEY (id);
--> statement-breakpoint
ALTER TABLE ONLY public.wishlist_items
    ADD CONSTRAINT wishlist_items_user_deal_unique UNIQUE (user_id, deal_id);
--> statement-breakpoint
CREATE INDEX addresses_user_default_idx ON public.addresses USING btree (user_id, is_default);
--> statement-breakpoint
CREATE INDEX admin_assignments_admin_idx ON public.admin_assignments USING btree (admin_user_id);
--> statement-breakpoint
CREATE INDEX affiliate_admin_actions_target_idx ON public.affiliate_admin_actions USING btree (target_user_id, ts DESC);
--> statement-breakpoint
CREATE INDEX affiliate_enrollments_status_idx ON public.affiliate_enrollments USING btree (status);
--> statement-breakpoint
CREATE INDEX affiliate_enrollments_stripe_acct_idx ON public.affiliate_enrollments USING btree (stripe_account_id) WHERE (stripe_account_id IS NOT NULL);
--> statement-breakpoint
CREATE UNIQUE INDEX affiliate_entries_idem_uq ON public.affiliate_entries USING btree (entry_type, source_type, source_id);
--> statement-breakpoint
CREATE INDEX affiliate_entries_source_idx ON public.affiliate_entries USING btree (source_type, source_id);
--> statement-breakpoint
CREATE INDEX affiliate_entries_unconsumed_idx ON public.affiliate_entries USING btree (source_type, source_id) WHERE (consumed_at IS NULL);
--> statement-breakpoint
CREATE UNIQUE INDEX affiliate_payouts_one_active_idx ON public.affiliate_payouts USING btree (user_id) WHERE (status = ANY (ARRAY['requested'::public.affiliate_payout_status, 'approved'::public.affiliate_payout_status, 'processing'::public.affiliate_payout_status]));
--> statement-breakpoint
CREATE UNIQUE INDEX affiliate_payouts_settlement_evidence_uq ON public.affiliate_payouts USING btree (settlement_rail, settlement_reference);
--> statement-breakpoint
CREATE INDEX affiliate_payouts_status_idx ON public.affiliate_payouts USING btree (status);
--> statement-breakpoint
CREATE INDEX affiliate_payouts_user_idx ON public.affiliate_payouts USING btree (user_id, requested_at DESC);
--> statement-breakpoint
CREATE UNIQUE INDEX agent_def_versions_def_ver_idx ON public.agent_definition_versions USING btree (definition_id, version);
--> statement-breakpoint
CREATE INDEX ai_interventions_parent_idx ON public.ai_interventions USING btree (parent_type, parent_id, created_at);
--> statement-breakpoint
CREATE INDEX cart_checkout_captures_status_idx ON public.cart_checkout_captures USING btree (status, created_at);
--> statement-breakpoint
CREATE INDEX cart_checkout_compensations_status_idx ON public.cart_checkout_compensations USING btree (status, updated_at);
--> statement-breakpoint
CREATE INDEX cart_items_user_idx ON public.cart_items USING btree (user_id);
--> statement-breakpoint
CREATE UNIQUE INDEX cart_line_cart_id_variant_id_key ON public.cart_line USING btree (cart_id, variant_id);
--> statement-breakpoint
CREATE UNIQUE INDEX cart_user_id_key ON public.cart USING btree (user_id) WHERE (user_id IS NOT NULL);
--> statement-breakpoint
CREATE INDEX case_offers_case_status_idx ON public.case_offers USING btree (case_id, status);
--> statement-breakpoint
CREATE UNIQUE INDEX category_translations_cat_locale_idx ON public.category_translations USING btree (category_id, locale);
--> statement-breakpoint
CREATE INDEX category_translations_locale_status_idx ON public.category_translations USING btree (locale, status);
--> statement-breakpoint
CREATE INDEX chat_messages_thread_created_idx ON public.chat_messages USING btree (thread_id, created_at DESC);
--> statement-breakpoint
CREATE INDEX chat_thread_participants_user_idx ON public.chat_thread_participants USING btree (user_id);
--> statement-breakpoint
CREATE INDEX chat_threads_last_message_idx ON public.chat_threads USING btree (last_message_at DESC);
--> statement-breakpoint
CREATE INDEX club_vendor_idx ON public.club_memberships USING btree (vendor_id);
--> statement-breakpoint
CREATE UNIQUE INDEX command_records_command_key_uq ON public.command_records USING btree (command_key);
--> statement-breakpoint
CREATE INDEX command_records_status_lease_expires_at_idx ON public.command_records USING btree (status, lease_expires_at);
--> statement-breakpoint
CREATE UNIQUE INDEX credit_ledger_idem_uq ON public.credit_ledger USING btree (entry_type, source_type, source_id);
--> statement-breakpoint
CREATE INDEX credit_ledger_pending_mature_idx ON public.credit_ledger USING btree (mature_at);
--> statement-breakpoint
CREATE INDEX credit_ledger_unswept_idx ON public.credit_ledger USING btree (mature_at) WHERE (swept_at IS NULL);
--> statement-breakpoint
CREATE INDEX credit_ledger_user_idx ON public.credit_ledger USING btree (user_id);
--> statement-breakpoint
CREATE INDEX deal_annotations_deal_idx ON public.deal_annotations USING btree (deal_id, is_visible);
--> statement-breakpoint
CREATE UNIQUE INDEX deal_categories_slug_uidx ON public.deal_categories USING btree (slug);
--> statement-breakpoint
CREATE INDEX deal_categories_type_active_idx ON public.deal_categories USING btree (deal_type, is_active, sort_order);
--> statement-breakpoint
CREATE INDEX deal_drafts_purge_idx ON public.deal_drafts USING btree (updated_at) WHERE (is_starred = false);
--> statement-breakpoint
CREATE INDEX deal_drafts_vendor_updated_idx ON public.deal_drafts USING btree (vendor_id, updated_at DESC NULLS LAST);
--> statement-breakpoint
CREATE INDEX deal_images_deal_idx ON public.deal_images USING btree (deal_id);
--> statement-breakpoint
CREATE UNIQUE INDEX deal_images_deal_primary_uniq ON public.deal_images USING btree (deal_id) WHERE ((is_primary = true) AND (sku_id IS NULL));
--> statement-breakpoint
CREATE INDEX deal_images_sku_idx ON public.deal_images USING btree (sku_id);
--> statement-breakpoint
CREATE UNIQUE INDEX deal_images_sku_primary_uniq ON public.deal_images USING btree (sku_id) WHERE ((is_primary = true) AND (sku_id IS NOT NULL));
--> statement-breakpoint
CREATE INDEX deal_skus_deal_hash_idx ON public.deal_skus USING btree (deal_id, option_ids_hash);
--> statement-breakpoint
CREATE INDEX deal_slug_redirects_deal_idx ON public.deal_slug_redirects USING btree (deal_id);
--> statement-breakpoint
CREATE UNIQUE INDEX deal_slug_redirects_locale_old_idx ON public.deal_slug_redirects USING btree (locale, old_slug);
--> statement-breakpoint
CREATE INDEX deal_tag_assignments_deal_idx ON public.deal_tag_assignments USING btree (deal_id);
--> statement-breakpoint
CREATE INDEX deal_tag_assignments_tag_idx ON public.deal_tag_assignments USING btree (tag_id);
--> statement-breakpoint
CREATE UNIQUE INDEX deal_tags_slug_uidx ON public.deal_tags USING btree (slug);
--> statement-breakpoint
CREATE UNIQUE INDEX deal_translations_deal_locale_idx ON public.deal_translations USING btree (deal_id, locale);
--> statement-breakpoint
CREATE UNIQUE INDEX deal_translations_locale_slug_idx ON public.deal_translations USING btree (locale, slug);
--> statement-breakpoint
CREATE INDEX deal_translations_locale_status_idx ON public.deal_translations USING btree (locale, status);
--> statement-breakpoint
CREATE UNIQUE INDEX deal_variant_axes_deal_visual_uniq ON public.deal_variant_axes USING btree (deal_id) WHERE (is_visual_axis = true);
--> statement-breakpoint
CREATE INDEX deals_category_state_created_idx ON public.deals USING btree (category_id, deal_state, created_at);
--> statement-breakpoint
CREATE INDEX deals_content_hash_idx ON public.deals USING btree (content_hash);
--> statement-breakpoint
CREATE INDEX deals_state_created_idx ON public.deals USING btree (deal_state, created_at);
--> statement-breakpoint
CREATE INDEX deals_state_window_end_idx ON public.deals USING btree (deal_state, window_end);
--> statement-breakpoint
CREATE INDEX deals_type_state_idx ON public.deals USING btree (deal_type, deal_state);
--> statement-breakpoint
CREATE INDEX deals_vendor_state_idx ON public.deals USING btree (vendor_id, deal_state);
--> statement-breakpoint
CREATE INDEX e2e_factory_commands_created_at_idx ON public.e2e_factory_commands USING btree (created_at);
--> statement-breakpoint
CREATE UNIQUE INDEX e2e_factory_entities_active_kind_entity_uq ON public.e2e_factory_entities USING btree (kind, entity_id) WHERE (deleted_at IS NULL);
--> statement-breakpoint
CREATE INDEX e2e_factory_entities_run_order_idx ON public.e2e_factory_entities USING btree (run_id, dependency_order);
--> statement-breakpoint
CREATE INDEX email_verifications_expires_at_idx ON public.email_verifications USING btree (expires_at);
--> statement-breakpoint
CREATE INDEX email_verifications_user_id_idx ON public.email_verifications USING btree (user_id);
--> statement-breakpoint
CREATE INDEX favorite_vendors_user_idx ON public.favorite_vendors USING btree (user_id);
--> statement-breakpoint
CREATE INDEX favorite_vendors_vendor_idx ON public.favorite_vendors USING btree (vendor_id);
--> statement-breakpoint
CREATE UNIQUE INDEX fraud_events_open_noref_uq ON public.fraud_events USING btree (user_id, decision_point, adapter) WHERE ((referral_id IS NULL) AND (resolved_at IS NULL));
--> statement-breakpoint
CREATE UNIQUE INDEX fraud_events_open_uq ON public.fraud_events USING btree (user_id, referral_id, decision_point, adapter) WHERE ((referral_id IS NOT NULL) AND (resolved_at IS NULL));
--> statement-breakpoint
CREATE INDEX fraud_events_referral_idx ON public.fraud_events USING btree (referral_id) WHERE (referral_id IS NOT NULL);
--> statement-breakpoint
CREATE INDEX fraud_events_resolved_ts_idx ON public.fraud_events USING btree (resolved_at, ts DESC);
--> statement-breakpoint
CREATE INDEX fraud_events_user_ts_idx ON public.fraud_events USING btree (user_id, ts DESC);
--> statement-breakpoint
CREATE INDEX group_deals_deal_id_idx ON public.group_deals USING btree (deal_id);
--> statement-breakpoint
CREATE INDEX group_deals_state_idx ON public.group_deals USING btree (group_state);
--> statement-breakpoint
CREATE INDEX group_reservations_group_deal_idx ON public.group_reservations USING btree (group_deal_id);
--> statement-breakpoint
CREATE INDEX group_reservations_status_idx ON public.group_reservations USING btree (status);
--> statement-breakpoint
CREATE INDEX group_reservations_user_idx ON public.group_reservations USING btree (user_id);
--> statement-breakpoint
CREATE INDEX group_tiers_group_deal_idx ON public.group_tiers USING btree (group_deal_id);
--> statement-breakpoint
CREATE INDEX group_waitlist_group_deal_idx ON public.group_waitlist USING btree (group_deal_id, "position");
--> statement-breakpoint
CREATE INDEX idx_sessions_prev_rt_hash ON public.sessions USING btree (previous_refresh_token_hash) WHERE (previous_refresh_token_hash IS NOT NULL);
--> statement-breakpoint
CREATE INDEX image_uploads_entity_idx ON public.image_uploads USING btree (entity_type, entity_id);
--> statement-breakpoint
CREATE INDEX image_uploads_purpose_idx ON public.image_uploads USING btree (purpose);
--> statement-breakpoint
CREATE UNIQUE INDEX invoices_purchase_provider_uniq ON public.invoices USING btree (order_line_id, provider);
--> statement-breakpoint
CREATE INDEX invoices_status_retry_idx ON public.invoices USING btree (status, retry_count) WHERE (status = 'failed'::text);
--> statement-breakpoint
CREATE INDEX ix_active_sku ON public.stock_reservation USING btree (sku_id) WHERE ((consumed_at IS NULL) AND (released_at IS NULL));
--> statement-breakpoint
CREATE INDEX ix_order_line_order ON public.order_line USING btree (order_id);
--> statement-breakpoint
CREATE INDEX ix_refund_intent_order ON public.refund_intent USING btree (order_id) WHERE (status = ANY (ARRAY['pending'::text, 'executed'::text]));
--> statement-breakpoint
CREATE INDEX languages_active_sort_idx ON public.languages USING btree (is_active, sort_order);
--> statement-breakpoint
CREATE UNIQUE INDEX languages_one_default_idx ON public.languages USING btree (is_default) WHERE (is_default = true);
--> statement-breakpoint
CREATE UNIQUE INDEX ledger_entries_idempotency_key_uq ON public.ledger_entries USING btree (idempotency_key);
--> statement-breakpoint
CREATE INDEX ledger_entry_vesting_mature_idx ON public.ledger_entry_vesting USING btree (mature_at) WHERE (swept_at IS NULL);
--> statement-breakpoint
CREATE INDEX ledger_recon_repairs_created_idx ON public.ledger_reconciliation_repairs USING btree (created_at);
--> statement-breakpoint
CREATE INDEX ledger_recon_repairs_user_idx ON public.ledger_reconciliation_repairs USING btree (user_id);
--> statement-breakpoint
CREATE INDEX live_notifications_expires_idx ON public.live_notifications USING btree (expires_at);
--> statement-breakpoint
CREATE INDEX live_notifications_user_unread_idx ON public.live_notifications USING btree (user_id, created_at DESC) WHERE (read_at IS NULL);
--> statement-breakpoint
CREATE INDEX llm_jobs_e2e_run_idx ON public.llm_jobs USING btree (e2e_run_id, status);
--> statement-breakpoint
CREATE INDEX llm_jobs_status_created_idx ON public.llm_jobs USING btree (status, created_at);
--> statement-breakpoint
CREATE INDEX llm_jobs_target_idx ON public.llm_jobs USING btree (target_id, job_type);
--> statement-breakpoint
CREATE UNIQUE INDEX llm_jobs_translation_dedup_idx ON public.llm_jobs USING btree (target_type, target_id, target_locale) WHERE (status = ANY (ARRAY['PENDING'::public.llm_job_status, 'RUNNING'::public.llm_job_status]));
--> statement-breakpoint
CREATE INDEX loyalty_vendor_idx ON public.loyalty_memberships USING btree (vendor_id);
--> statement-breakpoint
CREATE INDEX magic_links_expires_at_idx ON public.magic_links USING btree (expires_at);
--> statement-breakpoint
CREATE UNIQUE INDEX mts_user_trigger_uidx ON public.marketing_trigger_state USING btree (user_id, trigger_key);
--> statement-breakpoint
CREATE INDEX notifications_unresolved_idx ON public.notifications USING btree (recipient_type, recipient_id) WHERE (resolved_at IS NULL);
--> statement-breakpoint
CREATE UNIQUE INDEX order_payment_fees_order_kind_uq ON public.order_payment_fees USING btree (order_id, kind);
--> statement-breakpoint
CREATE INDEX order_share_attr_share_slug_idx ON public.order_share_attribution USING btree (share_slug_id);
--> statement-breakpoint
CREATE INDEX outbox_aggregate_feed_idx ON public.outbox USING btree (aggregate_type, aggregate_id, created_at);
--> statement-breakpoint
CREATE INDEX outbox_dead_at_idx ON public.outbox USING btree (dead_at);
--> statement-breakpoint
CREATE UNIQUE INDEX outbox_dedupe_key_uq ON public.outbox USING btree (dedupe_key);
--> statement-breakpoint
CREATE INDEX outbox_processed_at_idx ON public.outbox USING btree (processed_at);
--> statement-breakpoint
CREATE INDEX outbox_retry_idx ON public.outbox USING btree (processed_at, failed_at, retry_count);
--> statement-breakpoint
CREATE INDEX page_layouts_page_idx ON public.page_layouts USING btree (page);
--> statement-breakpoint
CREATE UNIQUE INDEX page_layouts_page_version_uk ON public.page_layouts USING btree (page, version);
--> statement-breakpoint
CREATE INDEX payment_finalize_checkpoint_pending_idx ON public.payment_finalize_checkpoints USING btree (status, updated_at);
--> statement-breakpoint
CREATE UNIQUE INDEX payment_finalize_checkpoint_provider_uniq ON public.payment_finalize_checkpoints USING btree (provider_payment_id) WHERE (effect_key = 'payment-claim'::text);
--> statement-breakpoint
CREATE INDEX payout_audit_log_kind_idx ON public.payout_audit_log USING btree (kind, created_at);
--> statement-breakpoint
CREATE INDEX payout_audit_log_vendor_idx ON public.payout_audit_log USING btree (vendor_id, created_at);
--> statement-breakpoint
CREATE INDEX payouts_status_idx ON public.payouts USING btree (status);
--> statement-breakpoint
CREATE INDEX payouts_vendor_id_idx ON public.payouts USING btree (vendor_id);
--> statement-breakpoint
CREATE INDEX promo_active_window_idx ON public.promo USING btree (active, starts_at, ends_at) WHERE (active = true);
--> statement-breakpoint
CREATE INDEX promo_allowlist_user_idx ON public.promo_allowlist USING btree (user_id);
--> statement-breakpoint
CREATE UNIQUE INDEX promo_code_uniq ON public.promo USING btree (upper(code));
--> statement-breakpoint
CREATE INDEX promo_redemptions_code_idx ON public.promo_redemptions USING btree (promo_code_id);
--> statement-breakpoint
CREATE INDEX promo_redemptions_user_code_idx ON public.promo_redemptions USING btree (user_id, promo_code_id);
--> statement-breakpoint
CREATE INDEX promo_redemptions_vendor_idx ON public.promo_redemptions USING btree (vendor_id, created_at);
--> statement-breakpoint
CREATE UNIQUE INDEX promo_reservations_active_uniq ON public.promo_reservations USING btree (promo_code_id, user_id, purchase_id) WHERE (status = ANY (ARRAY['pending'::text, 'finalized'::text]));
--> statement-breakpoint
CREATE INDEX promo_reservations_checkout_idx ON public.promo_reservations USING btree (checkout_scope);
--> statement-breakpoint
CREATE INDEX promo_reservations_promo_status_idx ON public.promo_reservations USING btree (promo_code_id, status);
--> statement-breakpoint
CREATE INDEX promo_reservations_purchase_idx ON public.promo_reservations USING btree (purchase_id);
--> statement-breakpoint
CREATE INDEX promo_reservations_user_idx ON public.promo_reservations USING btree (user_id, created_at);
--> statement-breakpoint
CREATE INDEX promo_vendor_idx ON public.promo USING btree (vendor_id) WHERE (vendor_id IS NOT NULL);
--> statement-breakpoint
CREATE INDEX purchases_deal_id_idx ON public.purchases_archived_pre_wipe USING btree (deal_id);
--> statement-breakpoint
CREATE INDEX purchases_pending_payment_idx ON public.purchases_archived_pre_wipe USING btree (payment_status, created_at) WHERE (payment_status = 'PENDING'::public.payment_status);
--> statement-breakpoint
CREATE UNIQUE INDEX purchases_provider_payment_id_uniq ON public.purchases_archived_pre_wipe USING btree (provider_payment_id) WHERE (provider_payment_id IS NOT NULL);
--> statement-breakpoint
CREATE INDEX purchases_ref_attribution_idx ON public.purchases_archived_pre_wipe USING btree (ref_attribution_id) WHERE (ref_attribution_id IS NOT NULL);
--> statement-breakpoint
CREATE INDEX purchases_share_slug_idx ON public.purchases_archived_pre_wipe USING btree (share_slug_id) WHERE (share_slug_id IS NOT NULL);
--> statement-breakpoint
CREATE INDEX purchases_status_created_idx ON public.purchases_archived_pre_wipe USING btree (payment_status, created_at);
--> statement-breakpoint
CREATE INDEX purchases_user_created_idx ON public.purchases_archived_pre_wipe USING btree (user_id, created_at);
--> statement-breakpoint
CREATE INDEX purchases_vendor_idx ON public.purchases_archived_pre_wipe USING btree (vendor_id);
--> statement-breakpoint
CREATE INDEX referral_link_stats_daily_day_idx ON public.referral_link_stats_daily USING btree (day);
--> statement-breakpoint
CREATE INDEX referral_links_owner_idx ON public.referral_links USING btree (owner_user_id);
--> statement-breakpoint
CREATE INDEX referrals_referrer_idx ON public.referrals USING btree (referrer_user_id);
--> statement-breakpoint
CREATE INDEX referrals_referrer_status_idx ON public.referrals USING btree (referrer_user_id, status);
--> statement-breakpoint
CREATE INDEX refund_intent_line_ext_order_line_idx ON public.refund_intent_line_ext USING btree (order_line_id);
--> statement-breakpoint
CREATE INDEX refunds_purchase_id_idx ON public.refunds USING btree (purchase_id);
--> statement-breakpoint
CREATE INDEX return_events_return_idx ON public.return_events USING btree (return_id, created_at);
--> statement-breakpoint
CREATE INDEX returns_buyer_idx ON public.returns USING btree (buyer_id, created_at);
--> statement-breakpoint
CREATE UNIQUE INDEX returns_purchase_open_uniq ON public.returns USING btree (order_line_id) WHERE (status <> ALL (ARRAY['refunded'::public.return_status, 'denied'::public.return_status, 'cancelled'::public.return_status, 'expired'::public.return_status]));
--> statement-breakpoint
CREATE INDEX returns_status_buyer_ship_idx ON public.returns USING btree (status, buyer_ship_by);
--> statement-breakpoint
CREATE INDEX returns_status_vendor_inspect_idx ON public.returns USING btree (status, vendor_inspect_by);
--> statement-breakpoint
CREATE INDEX returns_status_vendor_respond_idx ON public.returns USING btree (status, vendor_respond_by);
--> statement-breakpoint
CREATE INDEX returns_vendor_idx ON public.returns USING btree (vendor_id, created_at);
--> statement-breakpoint
CREATE INDEX review_product_status_idx ON public.review USING btree (product_id, status);
--> statement-breakpoint
CREATE INDEX review_translations_locale_status_idx ON public.review_translations USING btree (locale, status);
--> statement-breakpoint
CREATE UNIQUE INDEX review_translations_review_locale_idx ON public.review_translations USING btree (review_id, locale);
--> statement-breakpoint
CREATE UNIQUE INDEX review_user_product_purchase_uq ON public.review USING btree (user_id, product_id, purchase_id);
--> statement-breakpoint
CREATE INDEX reviews_deal_visible_idx ON public.reviews USING btree (deal_id, is_visible);
--> statement-breakpoint
CREATE INDEX reviews_vendor_visible_idx ON public.reviews USING btree (vendor_id, is_visible);
--> statement-breakpoint
CREATE INDEX sessions_expires_at_idx ON public.sessions USING btree (expires_at);
--> statement-breakpoint
CREATE UNIQUE INDEX sessions_refresh_token_hash_unique ON public.sessions USING btree (refresh_token_hash);
--> statement-breakpoint
CREATE INDEX share_audience_daily_day_idx ON public.share_audience_daily USING btree (day);
--> statement-breakpoint
CREATE INDEX share_links_channel_idx ON public.share_links USING btree (channel);
--> statement-breakpoint
CREATE INDEX share_links_created_idx ON public.share_links USING btree (created_at DESC);
--> statement-breakpoint
CREATE INDEX share_links_deal_idx ON public.share_links USING btree (deal_id);
--> statement-breakpoint
CREATE INDEX share_links_owner_idx ON public.share_links USING btree (owner_user_id);
--> statement-breakpoint
CREATE INDEX share_stats_day_idx ON public.share_link_stats_daily USING btree (day DESC);
--> statement-breakpoint
CREATE INDEX share_stats_deal_day_idx ON public.share_link_stats_daily USING btree (deal_id, day);
--> statement-breakpoint
CREATE INDEX shipment_events_shipment_idx ON public.shipment_events USING btree (shipment_id, created_at);
--> statement-breakpoint
CREATE UNIQUE INDEX shipment_purchases_purchase_unique ON public.shipment_purchases USING btree (order_line_id);
--> statement-breakpoint
CREATE INDEX shipments_auto_confirm_idx ON public.shipments USING btree (auto_confirm_at);
--> statement-breakpoint
CREATE INDEX shipments_buyer_status_idx ON public.shipments USING btree (buyer_id, status);
--> statement-breakpoint
CREATE INDEX shipments_status_handle_idx ON public.shipments USING btree (status, handle_by_at);
--> statement-breakpoint
CREATE INDEX shipments_tracking_idx ON public.shipments USING btree (carrier, tracking_number);
--> statement-breakpoint
CREATE INDEX shipments_vendor_status_idx ON public.shipments USING btree (vendor_id, status);
--> statement-breakpoint
CREATE INDEX shipping_addresses_city_idx ON public.shipping_addresses USING btree (city_code);
--> statement-breakpoint
CREATE INDEX shipping_addresses_user_default_idx ON public.shipping_addresses USING btree (user_id, is_default);
--> statement-breakpoint
CREATE INDEX sku_qty_tiers_deal_sku_idx ON public.sku_qty_tiers USING btree (deal_sku_id);
--> statement-breakpoint
CREATE INDEX slug_history_entity_idx ON public.slug_history USING btree (entity_type, entity_id);
--> statement-breakpoint
CREATE UNIQUE INDEX slug_history_lookup_uidx ON public.slug_history USING btree (entity_type, old_slug);
--> statement-breakpoint
CREATE INDEX stock_reservations_expires_idx ON public.stock_reservations USING btree (expires_at) WHERE ((consumed_at IS NULL) AND (released_at IS NULL));
--> statement-breakpoint
CREATE INDEX stock_reservations_sku_active_idx ON public.stock_reservations USING btree (sku_id) WHERE ((consumed_at IS NULL) AND (released_at IS NULL));
--> statement-breakpoint
CREATE INDEX stock_watchlist_deal_pending_idx ON public.stock_watchlist USING btree (deal_id) WHERE (notified_at IS NULL);
--> statement-breakpoint
CREATE UNIQUE INDEX stock_watchlist_user_deal_null_sku_uniq ON public.stock_watchlist USING btree (user_id, deal_id) WHERE (sku_id IS NULL);
--> statement-breakpoint
CREATE UNIQUE INDEX stock_watchlist_user_deal_sku_uniq ON public.stock_watchlist USING btree (user_id, deal_id, sku_id) WHERE (sku_id IS NOT NULL);
--> statement-breakpoint
CREATE INDEX support_attachments_parent_idx ON public.support_attachments USING btree (parent_type, parent_id);
--> statement-breakpoint
CREATE UNIQUE INDEX support_kb_article_translations_article_locale_idx ON public.support_kb_article_translations USING btree (article_id, locale);
--> statement-breakpoint
CREATE UNIQUE INDEX support_kb_article_translations_locale_slug_idx ON public.support_kb_article_translations USING btree (locale, slug);
--> statement-breakpoint
CREATE INDEX support_kb_article_translations_locale_status_idx ON public.support_kb_article_translations USING btree (locale, status);
--> statement-breakpoint
CREATE INDEX support_kb_articles_active_idx ON public.support_kb_articles USING btree (is_active);
--> statement-breakpoint
CREATE INDEX support_kb_articles_category_idx ON public.support_kb_articles USING btree (category);
--> statement-breakpoint
CREATE UNIQUE INDEX support_kb_articles_slug_idx ON public.support_kb_articles USING btree (slug);
--> statement-breakpoint
CREATE INDEX support_kb_articles_tags_gin_idx ON public.support_kb_articles USING gin (tags);
--> statement-breakpoint
CREATE UNIQUE INDEX support_kb_embeddings_article_chunk_idx ON public.support_kb_embeddings USING btree (article_id, chunk_index);
--> statement-breakpoint
CREATE INDEX support_kb_embeddings_vector_idx ON public.support_kb_embeddings USING hnsw (embedding public.vector_cosine_ops);
--> statement-breakpoint
CREATE INDEX support_messages_parent_idx ON public.support_messages USING btree (parent_type, parent_id, created_at);
--> statement-breakpoint
CREATE INDEX support_state_transitions_parent_idx ON public.support_state_transitions USING btree (parent_type, parent_id, created_at);
--> statement-breakpoint
CREATE INDEX support_tickets_opener_status_idx ON public.support_tickets USING btree (opener_id, status);
--> statement-breakpoint
CREATE INDEX support_tickets_status_updated_idx ON public.support_tickets USING btree (status, updated_at);
--> statement-breakpoint
CREATE UNIQUE INDEX system_health_unresolved_per_entity ON public.system_health_findings USING btree (kind, entity_type, entity_id) WHERE (resolved_at IS NULL);
--> statement-breakpoint
CREATE INDEX tag_translations_locale_status_idx ON public.tag_translations USING btree (locale, status);
--> statement-breakpoint
CREATE UNIQUE INDEX tag_translations_tag_locale_idx ON public.tag_translations USING btree (tag_id, locale);
--> statement-breakpoint
CREATE UNIQUE INDEX tax_documents_purchase_side_uq ON public.tax_documents USING btree (order_line_id, side);
--> statement-breakpoint
CREATE INDEX tm_last_used_idx ON public.translation_memory USING btree (last_used_at);
--> statement-breakpoint
CREATE INDEX tm_locale_pair_idx ON public.translation_memory USING btree (source_locale, target_locale);
--> statement-breakpoint
CREATE INDEX translation_glossary_mode_locale_idx ON public.translation_glossary USING btree (mode, locale);
--> statement-breakpoint
CREATE INDEX translation_jobs_deal_idx ON public.translation_jobs USING btree (deal_id);
--> statement-breakpoint
CREATE UNIQUE INDEX translation_jobs_deal_locale_active_idx ON public.translation_jobs USING btree (deal_id, target_locale) WHERE (status = ANY (ARRAY['PENDING'::text, 'RUNNING'::text, 'PENDING_BUDGET'::text]));
--> statement-breakpoint
CREATE INDEX translation_jobs_scheduled_at_idx ON public.translation_jobs USING btree (scheduled_at);
--> statement-breakpoint
CREATE INDEX translation_jobs_status_idx ON public.translation_jobs USING btree (status);
--> statement-breakpoint
CREATE UNIQUE INDEX tx_cases_purchase_open_uniq ON public.transaction_cases USING btree (order_line_id) WHERE (status <> ALL (ARRAY['resolved'::public.support_case_status, 'closed'::public.support_case_status]));
--> statement-breakpoint
CREATE INDEX tx_cases_status_autoclose_idx ON public.transaction_cases USING btree (status, autoclose_at);
--> statement-breakpoint
CREATE INDEX tx_cases_status_sla_ai_idx ON public.transaction_cases USING btree (status, sla_ai_due_at);
--> statement-breakpoint
CREATE INDEX tx_cases_status_sla_human_idx ON public.transaction_cases USING btree (status, sla_human_due_at);
--> statement-breakpoint
CREATE INDEX tx_cases_status_vendor_window_idx ON public.transaction_cases USING btree (status, vendor_window_expires_at);
--> statement-breakpoint
CREATE UNIQUE INDEX uq_access_grant_owner ON public.access_grant USING btree (order_id, item_id, owner_key);
--> statement-breakpoint
CREATE UNIQUE INDEX uq_active_holder ON public.stock_reservation USING btree (sku_id, holder_ref) WHERE ((consumed_at IS NULL) AND (released_at IS NULL));
--> statement-breakpoint
CREATE UNIQUE INDEX uq_order_idempotency_key ON public."order" USING btree (idempotency_key);
--> statement-breakpoint
CREATE UNIQUE INDEX uq_order_step ON public.order_step USING btree (order_id, step_id);
--> statement-breakpoint
CREATE UNIQUE INDEX uq_promo_code_platform ON public.promo USING btree (code) WHERE (vendor_id IS NULL);
--> statement-breakpoint
CREATE UNIQUE INDEX uq_promo_code_vendor ON public.promo USING btree (code, vendor_id) WHERE (vendor_id IS NOT NULL);
--> statement-breakpoint
CREATE UNIQUE INDEX uq_refund_intent_seq ON public.refund_intent USING btree (order_id, seq);
--> statement-breakpoint
CREATE UNIQUE INDEX uq_voucher_unit ON public.voucher USING btree (order_id, line_id, unit_index);
--> statement-breakpoint
CREATE INDEX user_recently_viewed_user_time_idx ON public.user_recently_viewed USING btree (user_id, viewed_at DESC);
--> statement-breakpoint
CREATE INDEX users_email_canonical_idx ON public.users USING btree (email_canonical_index) WHERE (email_canonical_index IS NOT NULL);
--> statement-breakpoint
CREATE INDEX users_email_index_idx ON public.users USING btree (email_index);
--> statement-breakpoint
CREATE INDEX users_phone_index_idx ON public.users USING btree (phone_index);
--> statement-breakpoint
CREATE INDEX users_preferred_city_code_idx ON public.users USING btree (preferred_city_code) WHERE (preferred_city_code IS NOT NULL);
--> statement-breakpoint
CREATE UNIQUE INDEX users_stripe_customer_id_uniq ON public.users USING btree (stripe_customer_id) WHERE (stripe_customer_id IS NOT NULL);
--> statement-breakpoint
CREATE INDEX vendor_business_types_type_idx ON public.vendor_business_types USING btree (business_type_id);
--> statement-breakpoint
CREATE INDEX vendor_payout_releases_batch_idx ON public.vendor_payout_releases USING btree (payout_batch_key);
--> statement-breakpoint
CREATE INDEX vendor_payout_releases_due_idx ON public.vendor_payout_releases USING btree (status, release_at);
--> statement-breakpoint
CREATE UNIQUE INDEX vendor_payout_releases_purchase_uniq ON public.vendor_payout_releases USING btree (order_line_id);
--> statement-breakpoint
CREATE INDEX vendor_payout_releases_vendor_idx ON public.vendor_payout_releases USING btree (vendor_id, status);
--> statement-breakpoint
CREATE INDEX vendor_recommendations_vendor_created_idx ON public.vendor_recommendations USING btree (vendor_id, created_at);
--> statement-breakpoint
CREATE INDEX vendor_translations_locale_status_idx ON public.vendor_translations USING btree (locale, status);
--> statement-breakpoint
CREATE UNIQUE INDEX vendor_translations_vendor_locale_idx ON public.vendor_translations USING btree (vendor_id, locale);
--> statement-breakpoint
CREATE INDEX vendors_owner_user_id_idx ON public.vendors USING btree (owner_user_id);
--> statement-breakpoint
CREATE UNIQUE INDEX vendors_stripe_account_id_uniq ON public.vendors USING btree (stripe_account_id) WHERE (stripe_account_id IS NOT NULL);
--> statement-breakpoint
CREATE INDEX wishlist_items_deal_idx ON public.wishlist_items USING btree (deal_id);
--> statement-breakpoint
CREATE INDEX wishlist_items_user_idx ON public.wishlist_items USING btree (user_id);
--> statement-breakpoint
CREATE INDEX zip_misses_city_idx ON public.address_zip_cache_misses USING btree (city_code, created_at);
--> statement-breakpoint
CREATE INDEX zip_misses_resolution_idx ON public.address_zip_cache_misses USING btree (resolution, created_at);
--> statement-breakpoint
CREATE TRIGGER deal_complete_for_active BEFORE INSERT OR UPDATE ON public.deals FOR EACH ROW EXECUTE FUNCTION public.assert_deal_complete_for_active();
--> statement-breakpoint
ALTER TABLE ONLY public.address_zip_cache_misses
    ADD CONSTRAINT address_zip_cache_misses_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.addresses
    ADD CONSTRAINT addresses_user_id_users_id_fk FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.admin_actions
    ADD CONSTRAINT admin_actions_admin_id_users_id_fk FOREIGN KEY (admin_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.admin_assignments
    ADD CONSTRAINT admin_assignments_admin_user_id_fkey FOREIGN KEY (admin_user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.affiliate_admin_actions
    ADD CONSTRAINT affiliate_admin_actions_admin_user_id_fkey FOREIGN KEY (admin_user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.affiliate_admin_actions
    ADD CONSTRAINT affiliate_admin_actions_target_user_id_fkey FOREIGN KEY (target_user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.affiliate_enrollments
    ADD CONSTRAINT affiliate_enrollments_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE RESTRICT;
--> statement-breakpoint
ALTER TABLE ONLY public.affiliate_entries
    ADD CONSTRAINT affiliate_entries_referral_id_referrals_id_fk FOREIGN KEY (referral_id) REFERENCES public.referrals(id);
--> statement-breakpoint
ALTER TABLE ONLY public.affiliate_payouts
    ADD CONSTRAINT affiliate_payouts_approved_by_user_id_fkey FOREIGN KEY (approved_by_user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.affiliate_payouts
    ADD CONSTRAINT affiliate_payouts_enrollment_id_fkey FOREIGN KEY (enrollment_id) REFERENCES public.affiliate_enrollments(id);
--> statement-breakpoint
ALTER TABLE ONLY public.affiliate_payouts
    ADD CONSTRAINT affiliate_payouts_ledger_entry_id_fkey FOREIGN KEY (ledger_entry_id) REFERENCES public.credit_ledger(id);
--> statement-breakpoint
ALTER TABLE ONLY public.affiliate_payouts
    ADD CONSTRAINT affiliate_payouts_settled_by_user_id_users_id_fk FOREIGN KEY (settled_by_user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.affiliate_payouts
    ADD CONSTRAINT affiliate_payouts_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.agent_definition_versions
    ADD CONSTRAINT agent_definition_versions_changed_by_fkey FOREIGN KEY (changed_by) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.agent_definition_versions
    ADD CONSTRAINT agent_definition_versions_definition_id_fkey FOREIGN KEY (definition_id) REFERENCES public.agent_definitions(id);
--> statement-breakpoint
ALTER TABLE ONLY public.business_hours
    ADD CONSTRAINT business_hours_vendor_id_vendors_id_fk FOREIGN KEY (vendor_id) REFERENCES public.vendors(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.cart_items
    ADD CONSTRAINT cart_items_deal_sku_id_deal_skus_id_fk FOREIGN KEY (deal_sku_id) REFERENCES public.deal_skus(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.cart_items
    ADD CONSTRAINT cart_items_user_id_users_id_fk FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.case_offers
    ADD CONSTRAINT case_offers_case_id_transaction_cases_id_fk FOREIGN KEY (case_id) REFERENCES public.transaction_cases(id);
--> statement-breakpoint
ALTER TABLE ONLY public.case_resolutions
    ADD CONSTRAINT case_resolutions_case_id_transaction_cases_id_fk FOREIGN KEY (case_id) REFERENCES public.transaction_cases(id);
--> statement-breakpoint
ALTER TABLE ONLY public.category_translations
    ADD CONSTRAINT category_translations_category_id_deal_categories_id_fk FOREIGN KEY (category_id) REFERENCES public.deal_categories(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.category_translations
    ADD CONSTRAINT category_translations_locale_languages_code_fk FOREIGN KEY (locale) REFERENCES public.languages(code);
--> statement-breakpoint
ALTER TABLE ONLY public.chat_messages
    ADD CONSTRAINT chat_messages_sender_user_id_fkey FOREIGN KEY (sender_user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.chat_messages
    ADD CONSTRAINT chat_messages_thread_id_fkey FOREIGN KEY (thread_id) REFERENCES public.chat_threads(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.chat_thread_participants
    ADD CONSTRAINT chat_thread_participants_thread_id_fkey FOREIGN KEY (thread_id) REFERENCES public.chat_threads(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.chat_thread_participants
    ADD CONSTRAINT chat_thread_participants_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.chat_threads
    ADD CONSTRAINT chat_threads_created_by_fkey FOREIGN KEY (created_by) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.club_memberships
    ADD CONSTRAINT club_memberships_joined_via_order_line_id_order_line_id_fk FOREIGN KEY (joined_via_order_line_id) REFERENCES public.order_line(id) ON DELETE SET NULL NOT VALID;
--> statement-breakpoint
ALTER TABLE ONLY public.club_memberships
    ADD CONSTRAINT club_memberships_user_id_users_id_fk FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.club_memberships
    ADD CONSTRAINT club_memberships_vendor_id_vendors_id_fk FOREIGN KEY (vendor_id) REFERENCES public.vendors(id);
--> statement-breakpoint
ALTER TABLE ONLY public.credit_ledger
    ADD CONSTRAINT credit_ledger_referral_id_referrals_id_fk FOREIGN KEY (referral_id) REFERENCES public.referrals(id);
--> statement-breakpoint
ALTER TABLE ONLY public.credit_ledger
    ADD CONSTRAINT credit_ledger_user_id_users_id_fk FOREIGN KEY (user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_annotations
    ADD CONSTRAINT deal_annotations_deal_id_deals_id_fk FOREIGN KEY (deal_id) REFERENCES public.deals(id);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_drafts
    ADD CONSTRAINT deal_drafts_vendor_id_vendors_id_fk FOREIGN KEY (vendor_id) REFERENCES public.vendors(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.deal_images
    ADD CONSTRAINT deal_images_deal_id_deals_id_fk FOREIGN KEY (deal_id) REFERENCES public.deals(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.deal_images
    ADD CONSTRAINT deal_images_draft_id_deal_drafts_id_fk FOREIGN KEY (draft_id) REFERENCES public.deal_drafts(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.deal_images
    ADD CONSTRAINT deal_images_sku_id_fkey FOREIGN KEY (sku_id) REFERENCES public.deal_skus(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.deal_item_config
    ADD CONSTRAINT deal_item_config_deal_id_fkey FOREIGN KEY (deal_id) REFERENCES public.deals(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.deal_item_config
    ADD CONSTRAINT deal_item_config_pickup_address_id_fkey FOREIGN KEY (pickup_address_id) REFERENCES public.vendor_addresses(id);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_skus
    ADD CONSTRAINT deal_skus_deal_id_deals_id_fk FOREIGN KEY (deal_id) REFERENCES public.deals(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.deal_slug_redirects
    ADD CONSTRAINT deal_slug_redirects_deal_id_deals_id_fk FOREIGN KEY (deal_id) REFERENCES public.deals(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.deal_slug_redirects
    ADD CONSTRAINT deal_slug_redirects_locale_languages_code_fk FOREIGN KEY (locale) REFERENCES public.languages(code);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_tag_assignments
    ADD CONSTRAINT deal_tag_assignments_deal_id_deals_id_fk FOREIGN KEY (deal_id) REFERENCES public.deals(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.deal_tag_assignments
    ADD CONSTRAINT deal_tag_assignments_tag_id_deal_tags_id_fk FOREIGN KEY (tag_id) REFERENCES public.deal_tags(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.deal_translations
    ADD CONSTRAINT deal_translations_deal_id_deals_id_fk FOREIGN KEY (deal_id) REFERENCES public.deals(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.deal_translations
    ADD CONSTRAINT deal_translations_locale_languages_code_fk FOREIGN KEY (locale) REFERENCES public.languages(code);
--> statement-breakpoint
ALTER TABLE ONLY public.deal_variant_axes
    ADD CONSTRAINT deal_variant_axes_deal_id_deals_id_fk FOREIGN KEY (deal_id) REFERENCES public.deals(id) ON DELETE RESTRICT;
--> statement-breakpoint
ALTER TABLE ONLY public.deal_variant_options
    ADD CONSTRAINT deal_variant_options_axis_id_deal_variant_axes_id_fk FOREIGN KEY (axis_id) REFERENCES public.deal_variant_axes(id) ON DELETE RESTRICT;
--> statement-breakpoint
ALTER TABLE ONLY public.deals
    ADD CONSTRAINT deals_category_id_deal_categories_id_fk FOREIGN KEY (category_id) REFERENCES public.deal_categories(id);
--> statement-breakpoint
ALTER TABLE ONLY public.deals
    ADD CONSTRAINT deals_personal_deal_for_user_id_users_id_fk FOREIGN KEY (personal_deal_for_user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.deals
    ADD CONSTRAINT deals_source_language_languages_code_fk FOREIGN KEY (source_language) REFERENCES public.languages(code);
--> statement-breakpoint
ALTER TABLE ONLY public.deals
    ADD CONSTRAINT deals_vendor_id_vendors_id_fk FOREIGN KEY (vendor_id) REFERENCES public.vendors(id);
--> statement-breakpoint
ALTER TABLE ONLY public.email_verifications
    ADD CONSTRAINT email_verifications_user_id_users_id_fk FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.favorite_vendors
    ADD CONSTRAINT favorite_vendors_user_id_users_id_fk FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.favorite_vendors
    ADD CONSTRAINT favorite_vendors_vendor_id_vendors_id_fk FOREIGN KEY (vendor_id) REFERENCES public.vendors(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.fraud_events
    ADD CONSTRAINT fraud_events_referral_id_fkey FOREIGN KEY (referral_id) REFERENCES public.referrals(id);
--> statement-breakpoint
ALTER TABLE ONLY public.fraud_events
    ADD CONSTRAINT fraud_events_resolved_by_fkey FOREIGN KEY (resolved_by) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.fraud_events
    ADD CONSTRAINT fraud_events_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.group_deals
    ADD CONSTRAINT group_deals_deal_id_deals_id_fk FOREIGN KEY (deal_id) REFERENCES public.deals(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.group_reservations
    ADD CONSTRAINT group_reservations_deal_id_deals_id_fk FOREIGN KEY (deal_id) REFERENCES public.deals(id);
--> statement-breakpoint
ALTER TABLE ONLY public.group_reservations
    ADD CONSTRAINT group_reservations_group_deal_id_group_deals_id_fk FOREIGN KEY (group_deal_id) REFERENCES public.group_deals(id);
--> statement-breakpoint
ALTER TABLE ONLY public.group_reservations
    ADD CONSTRAINT group_reservations_order_line_id_order_line_id_fk FOREIGN KEY (order_line_id) REFERENCES public.order_line(id) ON DELETE SET NULL NOT VALID;
--> statement-breakpoint
ALTER TABLE ONLY public.group_reservations
    ADD CONSTRAINT group_reservations_payment_method_id_payment_methods_id_fk FOREIGN KEY (payment_method_id) REFERENCES public.payment_methods(id);
--> statement-breakpoint
ALTER TABLE ONLY public.group_reservations
    ADD CONSTRAINT group_reservations_user_id_users_id_fk FOREIGN KEY (user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.group_tiers
    ADD CONSTRAINT group_tiers_group_deal_id_group_deals_id_fk FOREIGN KEY (group_deal_id) REFERENCES public.group_deals(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.group_waitlist
    ADD CONSTRAINT group_waitlist_group_deal_id_group_deals_id_fk FOREIGN KEY (group_deal_id) REFERENCES public.group_deals(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.group_waitlist
    ADD CONSTRAINT group_waitlist_payment_method_id_payment_methods_id_fk FOREIGN KEY (payment_method_id) REFERENCES public.payment_methods(id);
--> statement-breakpoint
ALTER TABLE ONLY public.group_waitlist
    ADD CONSTRAINT group_waitlist_user_id_users_id_fk FOREIGN KEY (user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.image_uploads
    ADD CONSTRAINT image_uploads_uploader_user_id_users_id_fk FOREIGN KEY (uploader_user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.image_uploads
    ADD CONSTRAINT image_uploads_uploader_vendor_id_vendors_id_fk FOREIGN KEY (uploader_vendor_id) REFERENCES public.vendors(id);
--> statement-breakpoint
ALTER TABLE ONLY public.invoices
    ADD CONSTRAINT invoices_buyer_id_fkey FOREIGN KEY (buyer_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.invoices
    ADD CONSTRAINT invoices_order_line_id_order_line_id_fk FOREIGN KEY (order_line_id) REFERENCES public.order_line(id) ON DELETE SET NULL NOT VALID;
--> statement-breakpoint
ALTER TABLE ONLY public.invoices
    ADD CONSTRAINT invoices_vendor_id_fkey FOREIGN KEY (vendor_id) REFERENCES public.vendors(id);
--> statement-breakpoint
ALTER TABLE ONLY public.kyc_verifications
    ADD CONSTRAINT kyc_verifications_user_id_users_id_fk FOREIGN KEY (user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.live_notifications
    ADD CONSTRAINT live_notifications_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.llm_jobs
    ADD CONSTRAINT llm_jobs_e2e_run_id_e2e_factory_runs_run_id_fk FOREIGN KEY (e2e_run_id) REFERENCES public.e2e_factory_runs(run_id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.llm_model_cache
    ADD CONSTRAINT llm_model_cache_provider_id_llm_providers_id_fk FOREIGN KEY (provider_id) REFERENCES public.llm_providers(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.llm_queues
    ADD CONSTRAINT llm_queues_updated_by_fkey FOREIGN KEY (updated_by) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.loyalty_memberships
    ADD CONSTRAINT loyalty_memberships_user_id_users_id_fk FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.loyalty_memberships
    ADD CONSTRAINT loyalty_memberships_vendor_id_vendors_id_fk FOREIGN KEY (vendor_id) REFERENCES public.vendors(id);
--> statement-breakpoint
ALTER TABLE ONLY public.marketing_trigger_state
    ADD CONSTRAINT marketing_trigger_state_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.order_guest_contact
    ADD CONSTRAINT order_guest_contact_order_id_fk FOREIGN KEY (order_id) REFERENCES public."order"(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.order_share_attribution
    ADD CONSTRAINT order_share_attribution_share_slug_id_share_links_id_fk FOREIGN KEY (share_slug_id) REFERENCES public.share_links(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.page_layout_pointers
    ADD CONSTRAINT page_layout_pointers_draft_updated_by_users_id_fk FOREIGN KEY (draft_updated_by) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.page_layout_pointers
    ADD CONSTRAINT page_layout_pointers_live_version_id_page_layouts_id_fk FOREIGN KEY (live_version_id) REFERENCES public.page_layouts(id);
--> statement-breakpoint
ALTER TABLE ONLY public.page_layout_pointers
    ADD CONSTRAINT page_layout_pointers_scheduled_version_id_page_layouts_id_fk FOREIGN KEY (scheduled_version_id) REFERENCES public.page_layouts(id);
--> statement-breakpoint
ALTER TABLE ONLY public.page_layouts
    ADD CONSTRAINT page_layouts_created_by_users_id_fk FOREIGN KEY (created_by) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.payment_methods
    ADD CONSTRAINT payment_methods_user_id_users_id_fk FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.payouts
    ADD CONSTRAINT payouts_vendor_id_fkey FOREIGN KEY (vendor_id) REFERENCES public.vendors(id);
--> statement-breakpoint
ALTER TABLE ONLY public.personal_deal_requests
    ADD CONSTRAINT personal_deal_requests_created_deal_id_deals_id_fk FOREIGN KEY (created_deal_id) REFERENCES public.deals(id);
--> statement-breakpoint
ALTER TABLE ONLY public.personal_deal_requests
    ADD CONSTRAINT personal_deal_requests_source_deal_id_deals_id_fk FOREIGN KEY (source_deal_id) REFERENCES public.deals(id);
--> statement-breakpoint
ALTER TABLE ONLY public.personal_deal_requests
    ADD CONSTRAINT personal_deal_requests_user_id_users_id_fk FOREIGN KEY (user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.personal_deal_requests
    ADD CONSTRAINT personal_deal_requests_vendor_id_vendors_id_fk FOREIGN KEY (vendor_id) REFERENCES public.vendors(id);
--> statement-breakpoint
ALTER TABLE ONLY public.platform_image_limits
    ADD CONSTRAINT platform_image_limits_updated_by_user_id_fkey FOREIGN KEY (updated_by_user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.promo_allowlist
    ADD CONSTRAINT promo_allowlist_promo_id_fkey FOREIGN KEY (promo_id) REFERENCES public.promo(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.promo_allowlist
    ADD CONSTRAINT promo_allowlist_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.promo
    ADD CONSTRAINT promo_author_user_id_fkey FOREIGN KEY (author_user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.promo_redemptions
    ADD CONSTRAINT promo_redemptions_order_line_id_order_line_id_fk FOREIGN KEY (order_line_id) REFERENCES public.order_line(id) ON DELETE SET NULL NOT VALID;
--> statement-breakpoint
ALTER TABLE ONLY public.promo_redemptions
    ADD CONSTRAINT promo_redemptions_promo_code_id_promo_id_fk FOREIGN KEY (promo_code_id) REFERENCES public.promo(id);
--> statement-breakpoint
ALTER TABLE ONLY public.promo_redemptions
    ADD CONSTRAINT promo_redemptions_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.promo_redemptions
    ADD CONSTRAINT promo_redemptions_vendor_id_fkey FOREIGN KEY (vendor_id) REFERENCES public.vendors(id);
--> statement-breakpoint
ALTER TABLE ONLY public.promo_reservations
    ADD CONSTRAINT promo_reservations_order_line_id_fkey FOREIGN KEY (order_line_id) REFERENCES public.order_line(id);
--> statement-breakpoint
ALTER TABLE ONLY public.promo_reservations
    ADD CONSTRAINT promo_reservations_promo_code_id_fkey FOREIGN KEY (promo_code_id) REFERENCES public.promo(id);
--> statement-breakpoint
ALTER TABLE ONLY public.promo_reservations
    ADD CONSTRAINT promo_reservations_purchase_id_fkey FOREIGN KEY (purchase_id) REFERENCES public.order_line(id);
--> statement-breakpoint
ALTER TABLE ONLY public.promo_reservations
    ADD CONSTRAINT promo_reservations_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.promo_reservations
    ADD CONSTRAINT promo_reservations_vendor_id_fkey FOREIGN KEY (vendor_id) REFERENCES public.vendors(id);
--> statement-breakpoint
ALTER TABLE ONLY public.purchase_messages
    ADD CONSTRAINT purchase_messages_order_line_id_order_line_id_fk FOREIGN KEY (order_line_id) REFERENCES public.order_line(id) ON DELETE CASCADE NOT VALID;
--> statement-breakpoint
ALTER TABLE ONLY public.purchases_archived_pre_wipe
    ADD CONSTRAINT purchases_deal_id_deals_id_fk FOREIGN KEY (deal_id) REFERENCES public.deals(id);
--> statement-breakpoint
ALTER TABLE ONLY public.purchases_archived_pre_wipe
    ADD CONSTRAINT purchases_deal_sku_id_deal_skus_id_fk FOREIGN KEY (deal_sku_id) REFERENCES public.deal_skus(id) ON DELETE RESTRICT;
--> statement-breakpoint
ALTER TABLE ONLY public.purchases_archived_pre_wipe
    ADD CONSTRAINT purchases_payment_method_id_payment_methods_id_fk FOREIGN KEY (payment_method_id) REFERENCES public.payment_methods(id);
--> statement-breakpoint
ALTER TABLE ONLY public.purchases_archived_pre_wipe
    ADD CONSTRAINT purchases_ref_attribution_id_fkey FOREIGN KEY (ref_attribution_id) REFERENCES public.referrals(id);
--> statement-breakpoint
ALTER TABLE ONLY public.purchases_archived_pre_wipe
    ADD CONSTRAINT purchases_share_slug_id_fkey FOREIGN KEY (share_slug_id) REFERENCES public.share_links(id) ON DELETE SET NULL;
--> statement-breakpoint
ALTER TABLE ONLY public.purchases_archived_pre_wipe
    ADD CONSTRAINT purchases_user_id_users_id_fk FOREIGN KEY (user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.purchases_archived_pre_wipe
    ADD CONSTRAINT purchases_vendor_id_vendors_id_fk FOREIGN KEY (vendor_id) REFERENCES public.vendors(id);
--> statement-breakpoint
ALTER TABLE ONLY public.push_subscriptions
    ADD CONSTRAINT push_subscriptions_user_id_users_id_fk FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.referral_link_stats_daily
    ADD CONSTRAINT referral_link_stats_daily_link_id_fkey FOREIGN KEY (link_id) REFERENCES public.referral_links(id);
--> statement-breakpoint
ALTER TABLE ONLY public.referral_links
    ADD CONSTRAINT referral_links_owner_user_id_users_id_fk FOREIGN KEY (owner_user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.referral_settings
    ADD CONSTRAINT referral_settings_updated_by_user_id_fkey FOREIGN KEY (updated_by_user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.referrals
    ADD CONSTRAINT referrals_link_id_referral_links_id_fk FOREIGN KEY (link_id) REFERENCES public.referral_links(id);
--> statement-breakpoint
ALTER TABLE ONLY public.referrals
    ADD CONSTRAINT referrals_referee_promo_code_id_promo_id_fk FOREIGN KEY (referee_promo_code_id) REFERENCES public.promo(id);
--> statement-breakpoint
ALTER TABLE ONLY public.referrals
    ADD CONSTRAINT referrals_referee_user_id_users_id_fk FOREIGN KEY (referee_user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.referrals
    ADD CONSTRAINT referrals_referrer_user_id_users_id_fk FOREIGN KEY (referrer_user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.refund_intent_line_ext
    ADD CONSTRAINT refund_intent_line_ext_order_line_id_fk FOREIGN KEY (order_line_id) REFERENCES public.order_line(id) ON DELETE RESTRICT;
--> statement-breakpoint
ALTER TABLE ONLY public.refund_intent_line_ext
    ADD CONSTRAINT refund_intent_line_ext_refund_intent_id_fk FOREIGN KEY (refund_intent_id) REFERENCES public.refund_intent(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.report_tickets
    ADD CONSTRAINT report_tickets_reporter_user_id_users_id_fk FOREIGN KEY (reporter_user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.return_events
    ADD CONSTRAINT return_events_return_id_fkey FOREIGN KEY (return_id) REFERENCES public.returns(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.returns
    ADD CONSTRAINT returns_buyer_id_fkey FOREIGN KEY (buyer_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.returns
    ADD CONSTRAINT returns_case_id_fkey FOREIGN KEY (case_id) REFERENCES public.transaction_cases(id);
--> statement-breakpoint
ALTER TABLE ONLY public.returns
    ADD CONSTRAINT returns_deal_id_fkey FOREIGN KEY (deal_id) REFERENCES public.deals(id);
--> statement-breakpoint
ALTER TABLE ONLY public.returns
    ADD CONSTRAINT returns_order_line_id_order_line_id_fk FOREIGN KEY (order_line_id) REFERENCES public.order_line(id) ON DELETE SET NULL NOT VALID;
--> statement-breakpoint
ALTER TABLE ONLY public.returns
    ADD CONSTRAINT returns_vendor_id_fkey FOREIGN KEY (vendor_id) REFERENCES public.vendors(id);
--> statement-breakpoint
ALTER TABLE ONLY public.review_removal_queue
    ADD CONSTRAINT review_removal_queue_review_id_reviews_id_fk FOREIGN KEY (review_id) REFERENCES public.reviews(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.review_removal_queue
    ADD CONSTRAINT review_removal_queue_submitted_by_vendors_id_fk FOREIGN KEY (submitted_by) REFERENCES public.vendors(id);
--> statement-breakpoint
ALTER TABLE ONLY public.review_translations
    ADD CONSTRAINT review_translations_locale_fkey FOREIGN KEY (locale) REFERENCES public.languages(code);
--> statement-breakpoint
ALTER TABLE ONLY public.review_translations
    ADD CONSTRAINT review_translations_review_id_fkey FOREIGN KEY (review_id) REFERENCES public.reviews(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.reviews
    ADD CONSTRAINT reviews_deal_id_fkey FOREIGN KEY (deal_id) REFERENCES public.deals(id);
--> statement-breakpoint
ALTER TABLE ONLY public.reviews
    ADD CONSTRAINT reviews_order_line_id_order_line_id_fk FOREIGN KEY (order_line_id) REFERENCES public.order_line(id) ON DELETE SET NULL NOT VALID;
--> statement-breakpoint
ALTER TABLE ONLY public.reviews
    ADD CONSTRAINT reviews_user_id_users_id_fk FOREIGN KEY (user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.reviews
    ADD CONSTRAINT reviews_vendor_id_vendors_id_fk FOREIGN KEY (vendor_id) REFERENCES public.vendors(id);
--> statement-breakpoint
ALTER TABLE ONLY public.sessions
    ADD CONSTRAINT sessions_user_id_users_id_fk FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.share_link_stats_daily
    ADD CONSTRAINT share_link_stats_daily_link_id_fkey FOREIGN KEY (link_id) REFERENCES public.share_links(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.share_links
    ADD CONSTRAINT share_links_deal_id_fkey FOREIGN KEY (deal_id) REFERENCES public.deals(id) ON DELETE SET NULL;
--> statement-breakpoint
ALTER TABLE ONLY public.share_links
    ADD CONSTRAINT share_links_owner_user_id_fkey FOREIGN KEY (owner_user_id) REFERENCES public.users(id) ON DELETE SET NULL;
--> statement-breakpoint
ALTER TABLE ONLY public.shipment_events
    ADD CONSTRAINT shipment_events_shipment_id_fkey FOREIGN KEY (shipment_id) REFERENCES public.shipments(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.shipment_purchases
    ADD CONSTRAINT shipment_purchases_order_line_id_order_line_id_fk FOREIGN KEY (order_line_id) REFERENCES public.order_line(id) ON DELETE RESTRICT NOT VALID;
--> statement-breakpoint
ALTER TABLE ONLY public.shipment_purchases
    ADD CONSTRAINT shipment_purchases_shipment_id_fkey FOREIGN KEY (shipment_id) REFERENCES public.shipments(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.shipments
    ADD CONSTRAINT shipments_buyer_id_fkey FOREIGN KEY (buyer_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.shipments
    ADD CONSTRAINT shipments_pickup_address_id_fkey FOREIGN KEY (pickup_address_id) REFERENCES public.vendor_addresses(id);
--> statement-breakpoint
ALTER TABLE ONLY public.shipments
    ADD CONSTRAINT shipments_shipping_address_id_fkey FOREIGN KEY (shipping_address_id) REFERENCES public.shipping_addresses(id);
--> statement-breakpoint
ALTER TABLE ONLY public.shipments
    ADD CONSTRAINT shipments_vendor_id_fkey FOREIGN KEY (vendor_id) REFERENCES public.vendors(id);
--> statement-breakpoint
ALTER TABLE ONLY public.shipping_addresses
    ADD CONSTRAINT shipping_addresses_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.sku_qty_tiers
    ADD CONSTRAINT sku_qty_tiers_deal_sku_id_deal_skus_id_fk FOREIGN KEY (deal_sku_id) REFERENCES public.deal_skus(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.standing_offers
    ADD CONSTRAINT standing_offers_vendor_id_vendors_id_fk FOREIGN KEY (vendor_id) REFERENCES public.vendors(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.stock_reservations
    ADD CONSTRAINT stock_reservations_sku_id_fkey FOREIGN KEY (sku_id) REFERENCES public.deal_skus(id);
--> statement-breakpoint
ALTER TABLE ONLY public.stock_reservations
    ADD CONSTRAINT stock_reservations_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.stock_watchlist
    ADD CONSTRAINT stock_watchlist_deal_id_fkey FOREIGN KEY (deal_id) REFERENCES public.deals(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.stock_watchlist
    ADD CONSTRAINT stock_watchlist_sku_id_fkey FOREIGN KEY (sku_id) REFERENCES public.deal_skus(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.stock_watchlist
    ADD CONSTRAINT stock_watchlist_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.support_attachments
    ADD CONSTRAINT support_attachments_message_id_support_messages_id_fk FOREIGN KEY (message_id) REFERENCES public.support_messages(id);
--> statement-breakpoint
ALTER TABLE ONLY public.support_attachments
    ADD CONSTRAINT support_attachments_uploader_id_users_id_fk FOREIGN KEY (uploader_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.support_kb_article_translations
    ADD CONSTRAINT support_kb_article_translations_article_id_support_kb_articles_ FOREIGN KEY (article_id) REFERENCES public.support_kb_articles(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.support_kb_article_translations
    ADD CONSTRAINT support_kb_article_translations_locale_languages_code_fk FOREIGN KEY (locale) REFERENCES public.languages(code);
--> statement-breakpoint
ALTER TABLE ONLY public.support_kb_articles
    ADD CONSTRAINT support_kb_articles_updated_by_users_id_fk FOREIGN KEY (updated_by) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.support_kb_embeddings
    ADD CONSTRAINT support_kb_embeddings_article_id_fkey FOREIGN KEY (article_id) REFERENCES public.support_kb_articles(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.support_tickets
    ADD CONSTRAINT support_tickets_agent_definition_id_fkey FOREIGN KEY (agent_definition_id) REFERENCES public.agent_definitions(id);
--> statement-breakpoint
ALTER TABLE ONLY public.support_tickets
    ADD CONSTRAINT support_tickets_assigned_agent_id_users_id_fk FOREIGN KEY (assigned_agent_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.support_tickets
    ADD CONSTRAINT support_tickets_opener_id_users_id_fk FOREIGN KEY (opener_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.support_tickets
    ADD CONSTRAINT support_tickets_related_order_id_order_id_fk FOREIGN KEY (related_order_id) REFERENCES public."order"(id) ON DELETE SET NULL NOT VALID;
--> statement-breakpoint
ALTER TABLE ONLY public.support_tickets
    ADD CONSTRAINT support_tickets_related_order_line_id_fkey FOREIGN KEY (related_order_line_id) REFERENCES public.order_line(id);
--> statement-breakpoint
ALTER TABLE ONLY public.tag_translations
    ADD CONSTRAINT tag_translations_locale_languages_code_fk FOREIGN KEY (locale) REFERENCES public.languages(code);
--> statement-breakpoint
ALTER TABLE ONLY public.tag_translations
    ADD CONSTRAINT tag_translations_tag_id_deal_tags_id_fk FOREIGN KEY (tag_id) REFERENCES public.deal_tags(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.transaction_cases
    ADD CONSTRAINT transaction_cases_agent_definition_id_fkey FOREIGN KEY (agent_definition_id) REFERENCES public.agent_definitions(id);
--> statement-breakpoint
ALTER TABLE ONLY public.transaction_cases
    ADD CONSTRAINT transaction_cases_assigned_agent_id_users_id_fk FOREIGN KEY (assigned_agent_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.transaction_cases
    ADD CONSTRAINT transaction_cases_customer_id_users_id_fk FOREIGN KEY (customer_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.transaction_cases
    ADD CONSTRAINT transaction_cases_order_line_id_order_line_id_fk FOREIGN KEY (order_line_id) REFERENCES public.order_line(id) ON DELETE SET NULL NOT VALID;
--> statement-breakpoint
ALTER TABLE ONLY public.transaction_cases
    ADD CONSTRAINT transaction_cases_vendor_id_vendors_id_fk FOREIGN KEY (vendor_id) REFERENCES public.vendors(id);
--> statement-breakpoint
ALTER TABLE ONLY public.translation_jobs
    ADD CONSTRAINT translation_jobs_deal_id_deals_id_fk FOREIGN KEY (deal_id) REFERENCES public.deals(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.translation_jobs
    ADD CONSTRAINT translation_jobs_target_locale_languages_code_fk FOREIGN KEY (target_locale) REFERENCES public.languages(code);
--> statement-breakpoint
ALTER TABLE ONLY public.translation_memory
    ADD CONSTRAINT translation_memory_source_locale_languages_code_fk FOREIGN KEY (source_locale) REFERENCES public.languages(code);
--> statement-breakpoint
ALTER TABLE ONLY public.translation_memory
    ADD CONSTRAINT translation_memory_target_locale_languages_code_fk FOREIGN KEY (target_locale) REFERENCES public.languages(code);
--> statement-breakpoint
ALTER TABLE ONLY public.user_gallery_images
    ADD CONSTRAINT user_gallery_images_order_line_id_order_line_id_fk FOREIGN KEY (order_line_id) REFERENCES public.order_line(id) ON DELETE SET NULL NOT VALID;
--> statement-breakpoint
ALTER TABLE ONLY public.user_gallery_images
    ADD CONSTRAINT user_gallery_images_user_id_users_id_fk FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.user_gallery_images
    ADD CONSTRAINT user_gallery_images_vendor_id_vendors_id_fk FOREIGN KEY (vendor_id) REFERENCES public.vendors(id);
--> statement-breakpoint
ALTER TABLE ONLY public.user_recently_viewed
    ADD CONSTRAINT user_recently_viewed_deal_id_fkey FOREIGN KEY (deal_id) REFERENCES public.deals(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.user_recently_viewed
    ADD CONSTRAINT user_recently_viewed_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.vendor_addresses
    ADD CONSTRAINT vendor_addresses_vendor_id_vendors_id_fk FOREIGN KEY (vendor_id) REFERENCES public.vendors(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.vendor_business_types
    ADD CONSTRAINT vendor_business_types_business_type_id_fkey FOREIGN KEY (business_type_id) REFERENCES public.business_types(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.vendor_business_types
    ADD CONSTRAINT vendor_business_types_vendor_id_fkey FOREIGN KEY (vendor_id) REFERENCES public.vendors(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.vendor_gallery_images
    ADD CONSTRAINT vendor_gallery_images_vendor_id_vendors_id_fk FOREIGN KEY (vendor_id) REFERENCES public.vendors(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.vendor_invoice_settings
    ADD CONSTRAINT vendor_invoice_settings_vendor_id_fkey FOREIGN KEY (vendor_id) REFERENCES public.vendors(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.vendor_payout_releases
    ADD CONSTRAINT vendor_payout_releases_order_line_id_order_line_id_fk FOREIGN KEY (order_line_id) REFERENCES public.order_line(id) ON DELETE SET NULL NOT VALID;
--> statement-breakpoint
ALTER TABLE ONLY public.vendor_payout_releases
    ADD CONSTRAINT vendor_payout_releases_vendor_id_fkey FOREIGN KEY (vendor_id) REFERENCES public.vendors(id);
--> statement-breakpoint
ALTER TABLE ONLY public.vendor_recommendations
    ADD CONSTRAINT vendor_recommendations_vendor_id_vendors_id_fk FOREIGN KEY (vendor_id) REFERENCES public.vendors(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.vendor_translations
    ADD CONSTRAINT vendor_translations_locale_languages_code_fk FOREIGN KEY (locale) REFERENCES public.languages(code);
--> statement-breakpoint
ALTER TABLE ONLY public.vendor_translations
    ADD CONSTRAINT vendor_translations_vendor_id_vendors_id_fk FOREIGN KEY (vendor_id) REFERENCES public.vendors(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.vendors
    ADD CONSTRAINT vendors_owner_user_id_users_id_fk FOREIGN KEY (owner_user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.wallet_balances
    ADD CONSTRAINT wallet_balances_user_id_users_id_fk FOREIGN KEY (user_id) REFERENCES public.users(id);
--> statement-breakpoint
ALTER TABLE ONLY public.wishlist_items
    ADD CONSTRAINT wishlist_items_deal_id_deals_id_fk FOREIGN KEY (deal_id) REFERENCES public.deals(id) ON DELETE CASCADE;
--> statement-breakpoint
ALTER TABLE ONLY public.wishlist_items
    ADD CONSTRAINT wishlist_items_user_id_users_id_fk FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE;
