--
-- PostgreSQL database dump
--

\restrict 5e1m4PNuOg4MdeK0WaimWUgWChhdkifajGmDuyLVHNdgxRokenI4nviT4z1dDjd

-- Dumped from database version 17.6
-- Dumped by pg_dump version 17.9

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET transaction_timeout = 0;
SET client_encoding = 'SQL_ASCII';
SET standard_conforming_strings = off;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET escape_string_warning = off;
SET row_security = off;

--
-- Name: public; Type: SCHEMA; Schema: -; Owner: -
--

CREATE SCHEMA public;


--
-- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: -
--

COMMENT ON SCHEMA public IS 'standard public schema';


--
-- Name: app_role; Type: TYPE; Schema: public; Owner: -
--

CREATE TYPE public.app_role AS ENUM (
    'admin',
    'agent'
);


--
-- Name: bump_support_ticket(); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION public.bump_support_ticket() RETURNS trigger
    LANGUAGE plpgsql SECURITY DEFINER
    SET search_path TO 'public'
    AS $$
BEGIN
  UPDATE public.support_tickets
  SET last_message_at = NEW.created_at,
      status = CASE WHEN NEW.from_admin THEN 'answered' ELSE 'open' END,
      updated_at = now()
  WHERE id = NEW.ticket_id;
  RETURN NEW;
END;
$$;


--
-- Name: has_active_subscription(uuid); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION public.has_active_subscription(_user_id uuid) RETURNS boolean
    LANGUAGE sql STABLE SECURITY DEFINER
    SET search_path TO 'public'
    AS $$
  SELECT EXISTS (
    SELECT 1 FROM public.subscriptions
    WHERE user_id = _user_id AND status = 'active' AND expires_at > now()
  )
$$;


--
-- Name: has_role(uuid, public.app_role); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION public.has_role(_user_id uuid, _role public.app_role) RETURNS boolean
    LANGUAGE sql STABLE SECURITY DEFINER
    SET search_path TO 'public'
    AS $$
  SELECT EXISTS (SELECT 1 FROM public.user_roles WHERE user_id = _user_id AND role = _role)
$$;


--
-- Name: is_support_staff(uuid); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION public.is_support_staff(_user_id uuid) RETURNS boolean
    LANGUAGE sql STABLE SECURITY DEFINER
    SET search_path TO 'public'
    AS $$
  SELECT public.has_role(_user_id, 'admin') OR EXISTS (
    SELECT 1 FROM public.staff_members
    WHERE user_id = _user_id AND active AND can_tickets
  )
$$;


--
-- Name: update_updated_at_column(); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION public.update_updated_at_column() RETURNS trigger
    LANGUAGE plpgsql
    SET search_path TO 'public'
    AS $$ BEGIN NEW.updated_at = now(); RETURN NEW; END; $$;


SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: agent_settings; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.agent_settings (
    user_id uuid NOT NULL,
    provider text DEFAULT 'faraz'::text NOT NULL,
    api_key text DEFAULT ''::text NOT NULL,
    sender text DEFAULT ''::text NOT NULL,
    reminder_template text DEFAULT '{name} عزیز، قسط شماره {seq} بیمه‌نامه {policy} به مبلغ {amount} ریال در تاریخ {due} سر می‌رسد.'::text NOT NULL,
    overdue_template text DEFAULT '{name} عزیز، قسط شماره {seq} بیمه‌نامه {policy} به مبلغ {amount} ریال سرسید {due} پرداخت نشده است. لطفاً تسویه فرمایید.'::text NOT NULL,
    auto_reminder boolean DEFAULT true NOT NULL,
    reminder_days integer DEFAULT 3 NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    receipt_template text DEFAULT '{name} عزیز، دریافت قسط شماره {seq} بیمه‌نامه {policy} به مبلغ {amount} ریال در تاریخ {date} تایید و ثبت گردید. با تشکر.'::text NOT NULL,
    pattern text DEFAULT ''::text NOT NULL,
    birthday_enabled boolean DEFAULT true NOT NULL,
    birthday_template text DEFAULT '{name} عزیز، زادروزتان مبارک! سالی پر از سلامتی و آرامش برای شما آرزو می‌کنیم.'::text NOT NULL,
    repeat_reminder boolean DEFAULT true NOT NULL,
    repeat_days integer DEFAULT 3 NOT NULL,
    username text DEFAULT ''::text NOT NULL,
    password text DEFAULT ''::text NOT NULL
);


--
-- Name: customers; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.customers (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    owner_id uuid NOT NULL,
    name text NOT NULL,
    phone text DEFAULT ''::text NOT NULL,
    national_id text DEFAULT ''::text NOT NULL,
    address text DEFAULT ''::text NOT NULL,
    note text DEFAULT ''::text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    referrer_name text DEFAULT ''::text NOT NULL,
    referrer_phone text DEFAULT ''::text NOT NULL,
    birth_date date,
    birthday_sent_at timestamp with time zone
);


--
-- Name: discount_codes; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.discount_codes (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    code text NOT NULL,
    percent integer DEFAULT 0 NOT NULL,
    amount_off bigint DEFAULT 0 NOT NULL,
    max_uses integer DEFAULT 0 NOT NULL,
    used_count integer DEFAULT 0 NOT NULL,
    expires_at timestamp with time zone,
    active boolean DEFAULT true NOT NULL,
    note text DEFAULT ''::text NOT NULL,
    created_by uuid,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    plan_id uuid,
    min_amount bigint DEFAULT 0 NOT NULL
);


--
-- Name: installment_payments; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.installment_payments (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    owner_id uuid NOT NULL,
    installment_id uuid NOT NULL,
    policy_id uuid,
    customer_id uuid,
    amount bigint DEFAULT 0 NOT NULL,
    paid_on date DEFAULT CURRENT_DATE NOT NULL,
    note text DEFAULT ''::text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    recorded_on date
);


--
-- Name: installments; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.installments (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    owner_id uuid NOT NULL,
    policy_id uuid NOT NULL,
    customer_id uuid NOT NULL,
    seq integer DEFAULT 1 NOT NULL,
    due_date date NOT NULL,
    amount bigint DEFAULT 0 NOT NULL,
    paid_amount bigint DEFAULT 0 NOT NULL,
    paid_date date,
    payment_code text DEFAULT ''::text NOT NULL,
    reminder_sent_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    note text DEFAULT ''::text NOT NULL
);


--
-- Name: invite_tokens; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.invite_tokens (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    token text NOT NULL,
    months integer DEFAULT 1 NOT NULL,
    note text DEFAULT ''::text NOT NULL,
    created_by uuid,
    used_by uuid,
    used_at timestamp with time zone,
    expires_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    days integer DEFAULT 0 NOT NULL
);


--
-- Name: ledger_entries; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.ledger_entries (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    owner_id uuid NOT NULL,
    kind text DEFAULT 'income'::text NOT NULL,
    amount bigint DEFAULT 0 NOT NULL,
    occurred_on date DEFAULT CURRENT_DATE NOT NULL,
    category text DEFAULT ''::text NOT NULL,
    note text DEFAULT ''::text NOT NULL,
    customer_id uuid,
    policy_id uuid,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    marketer_id uuid,
    CONSTRAINT ledger_entries_kind_check CHECK ((kind = ANY (ARRAY['income'::text, 'expense'::text, 'cash_in'::text, 'cash_out'::text])))
);


--
-- Name: login_otps; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.login_otps (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    phone text NOT NULL,
    code_hash text NOT NULL,
    attempts integer DEFAULT 0 NOT NULL,
    consumed boolean DEFAULT false NOT NULL,
    expires_at timestamp with time zone NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: marketer_payouts; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.marketer_payouts (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    owner_id uuid NOT NULL,
    marketer_id uuid NOT NULL,
    amount bigint DEFAULT 0 NOT NULL,
    paid_on date DEFAULT CURRENT_DATE NOT NULL,
    note text DEFAULT ''::text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: marketers; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.marketers (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    owner_id uuid NOT NULL,
    name text DEFAULT ''::text NOT NULL,
    phone text DEFAULT ''::text NOT NULL,
    national_id text DEFAULT ''::text NOT NULL,
    percent_receipt numeric DEFAULT 0 NOT NULL,
    fixed_receipt bigint DEFAULT 0 NOT NULL,
    percent_payout numeric DEFAULT 0 NOT NULL,
    fixed_payout bigint DEFAULT 0 NOT NULL,
    percent_policy numeric DEFAULT 0 NOT NULL,
    fixed_policy bigint DEFAULT 0 NOT NULL,
    active boolean DEFAULT true NOT NULL,
    note text DEFAULT ''::text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: payment_gateways; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.payment_gateways (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    title text DEFAULT ''::text NOT NULL,
    provider text DEFAULT 'zarinpal'::text NOT NULL,
    merchant_id text DEFAULT ''::text NOT NULL,
    sandbox boolean DEFAULT false NOT NULL,
    enabled boolean DEFAULT true NOT NULL,
    sort_order integer DEFAULT 0 NOT NULL,
    note text DEFAULT ''::text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: payments; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.payments (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    months integer DEFAULT 1 NOT NULL,
    amount bigint DEFAULT 0 NOT NULL,
    method text DEFAULT 'manual'::text NOT NULL,
    reference text DEFAULT ''::text NOT NULL,
    status text DEFAULT 'pending'::text NOT NULL,
    reviewed_by uuid,
    reviewed_at timestamp with time zone,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    discount_code text DEFAULT ''::text NOT NULL,
    discount_amount bigint DEFAULT 0 NOT NULL
);


--
-- Name: plans; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.plans (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    label text DEFAULT ''::text NOT NULL,
    months integer DEFAULT 1 NOT NULL,
    amount bigint DEFAULT 0 NOT NULL,
    sort_order integer DEFAULT 0 NOT NULL,
    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,
    compare_amount bigint DEFAULT 0 NOT NULL
);


--
-- Name: platform_ledger; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.platform_ledger (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    kind text DEFAULT 'income'::text NOT NULL,
    category text DEFAULT 'token'::text NOT NULL,
    amount bigint DEFAULT 0 NOT NULL,
    occurred_on date DEFAULT CURRENT_DATE NOT NULL,
    title text DEFAULT ''::text NOT NULL,
    buyer_name text DEFAULT ''::text NOT NULL,
    buyer_phone text DEFAULT ''::text NOT NULL,
    months integer DEFAULT 0 NOT NULL,
    reference text DEFAULT ''::text NOT NULL,
    note text DEFAULT ''::text NOT NULL,
    created_by uuid,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: platform_payment; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.platform_payment (
    id text DEFAULT 'main'::text NOT NULL,
    provider text DEFAULT 'zarinpal'::text NOT NULL,
    merchant_id text DEFAULT ''::text NOT NULL,
    sandbox boolean DEFAULT false NOT NULL,
    gateway_enabled boolean DEFAULT false NOT NULL,
    card_enabled boolean DEFAULT true NOT NULL,
    card_number text DEFAULT ''::text NOT NULL,
    card_holder text DEFAULT ''::text NOT NULL,
    card_bank text DEFAULT ''::text NOT NULL,
    note text DEFAULT ''::text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: platform_sms; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.platform_sms (
    id text DEFAULT 'main'::text NOT NULL,
    provider text DEFAULT 'supabase'::text NOT NULL,
    api_key text DEFAULT ''::text NOT NULL,
    sender text DEFAULT ''::text NOT NULL,
    template text DEFAULT 'کد ورود شما به بیمه لند: {code}'::text NOT NULL,
    code_length integer DEFAULT 5 NOT NULL,
    expire_seconds integer DEFAULT 180 NOT NULL,
    resend_seconds integer DEFAULT 60 NOT NULL,
    max_attempts integer DEFAULT 5 NOT NULL,
    enabled 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,
    pattern text DEFAULT ''::text NOT NULL,
    renew_template text DEFAULT 'همکار گرامی {name}، اشتراک شما در بیمه پی تا {days} روز دیگر ({date}) به پایان می‌رسد. برای تمدید وارد پنل شوید.'::text NOT NULL,
    renew_days integer DEFAULT 7 NOT NULL,
    renew_enabled boolean DEFAULT true NOT NULL,
    renew_repeat boolean DEFAULT true NOT NULL,
    renew_repeat_days integer DEFAULT 3 NOT NULL,
    username text DEFAULT ''::text NOT NULL,
    password text DEFAULT ''::text NOT NULL,
    birthday_enabled boolean DEFAULT true NOT NULL,
    birthday_template text DEFAULT 'همکار گرامی {name}، زادروزتان مبارک! تیم بیمه پی برایتان سالی پرخیر آرزو می‌کند.'::text NOT NULL
);


--
-- Name: policies; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.policies (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    owner_id uuid NOT NULL,
    customer_id uuid NOT NULL,
    policy_number text DEFAULT ''::text NOT NULL,
    kind text DEFAULT 'شخص ثالث'::text NOT NULL,
    insurer text DEFAULT 'بیمه ایران'::text NOT NULL,
    total_amount bigint DEFAULT 0 NOT NULL,
    prepaid bigint DEFAULT 0 NOT NULL,
    start_date date DEFAULT CURRENT_DATE NOT NULL,
    end_date date,
    installment_count integer DEFAULT 0 NOT NULL,
    vehicle text DEFAULT ''::text NOT NULL,
    plate text DEFAULT ''::text NOT NULL,
    unique_code text DEFAULT ''::text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    payment_mode text DEFAULT 'installment'::text NOT NULL,
    prepaid_collected boolean DEFAULT true NOT NULL,
    prepaid_paid_on date,
    referrer_name text DEFAULT ''::text NOT NULL,
    referrer_phone text DEFAULT ''::text NOT NULL,
    marketer_id uuid,
    prepaid_received bigint DEFAULT 0 NOT NULL
);


--
-- Name: profiles; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.profiles (
    id uuid NOT NULL,
    full_name text DEFAULT ''::text NOT NULL,
    agency_name text DEFAULT ''::text NOT NULL,
    phone text DEFAULT ''::text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    referral_code text DEFAULT ''::text NOT NULL,
    avatar_url text DEFAULT ''::text NOT NULL,
    birth_date date,
    birthday_sent_at timestamp with time zone,
    agency_code text DEFAULT ''::text NOT NULL,
    province text DEFAULT ''::text NOT NULL,
    city text DEFAULT ''::text NOT NULL,
    national_id text DEFAULT ''::text NOT NULL,
    discovery_source text DEFAULT ''::text NOT NULL,
    CONSTRAINT profiles_discovery_source_check CHECK (((discovery_source = ''::text) OR (discovery_source = ANY (ARRAY['social'::text, 'friends'::text, 'search'::text, 'advertising'::text])))),
    CONSTRAINT profiles_national_id_format_check CHECK (((national_id = ''::text) OR (national_id ~ '^[0-9]{10}$'::text))),
    CONSTRAINT profiles_phone_format_check CHECK (((phone = ''::text) OR (phone ~ '^09[0-9]{9}$'::text)))
);


--
-- Name: referrals; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.referrals (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    referrer_id uuid NOT NULL,
    referred_id uuid NOT NULL,
    days_awarded integer DEFAULT 14 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: site_settings; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.site_settings (
    id text DEFAULT 'main'::text NOT NULL,
    data 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
);


--
-- Name: sms_logs; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.sms_logs (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    owner_id uuid NOT NULL,
    customer_id uuid,
    installment_id uuid,
    phone text NOT NULL,
    body text NOT NULL,
    status text DEFAULT 'sent'::text NOT NULL,
    error text DEFAULT ''::text NOT NULL,
    kind text DEFAULT 'manual'::text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    delivered_at timestamp with time zone,
    provider_message_id text DEFAULT ''::text NOT NULL
);


--
-- Name: staff_members; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.staff_members (
    user_id uuid NOT NULL,
    full_name text DEFAULT ''::text NOT NULL,
    email text DEFAULT ''::text NOT NULL,
    can_tickets boolean DEFAULT true NOT NULL,
    can_registrations boolean DEFAULT false NOT NULL,
    active boolean DEFAULT true NOT NULL,
    note text DEFAULT ''::text NOT NULL,
    created_by uuid,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: subscription_orders; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.subscription_orders (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    full_name text DEFAULT ''::text NOT NULL,
    agency_name text DEFAULT ''::text NOT NULL,
    phone text DEFAULT ''::text NOT NULL,
    email text DEFAULT ''::text NOT NULL,
    plan_id uuid,
    plan_label text DEFAULT ''::text NOT NULL,
    months integer DEFAULT 1 NOT NULL,
    amount bigint DEFAULT 0 NOT NULL,
    discount_code text DEFAULT ''::text NOT NULL,
    discount_amount bigint DEFAULT 0 NOT NULL,
    method text DEFAULT 'card'::text NOT NULL,
    reference text DEFAULT ''::text NOT NULL,
    status text DEFAULT 'pending'::text NOT NULL,
    token text DEFAULT ''::text NOT NULL,
    authority text DEFAULT ''::text NOT NULL,
    note text DEFAULT ''::text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: subscriptions; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.subscriptions (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    months integer DEFAULT 1 NOT NULL,
    status text DEFAULT 'active'::text NOT NULL,
    starts_at timestamp with time zone DEFAULT now() NOT NULL,
    expires_at timestamp with time zone NOT NULL,
    amount bigint DEFAULT 0 NOT NULL,
    note text DEFAULT ''::text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    kind text DEFAULT 'standard'::text NOT NULL,
    renew_notice_sent_at timestamp with time zone
);


--
-- Name: support_messages; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.support_messages (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    ticket_id uuid NOT NULL,
    sender_id uuid,
    from_admin boolean DEFAULT false NOT NULL,
    body text DEFAULT ''::text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: support_tickets; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.support_tickets (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    owner_id uuid NOT NULL,
    subject text DEFAULT ''::text NOT NULL,
    status text DEFAULT 'open'::text NOT NULL,
    priority text DEFAULT 'normal'::text NOT NULL,
    last_message_at timestamp with time zone DEFAULT now() NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: tutorials; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.tutorials (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    title text DEFAULT ''::text NOT NULL,
    body text DEFAULT ''::text NOT NULL,
    video_url text DEFAULT ''::text NOT NULL,
    sort_order integer DEFAULT 0 NOT NULL,
    active boolean DEFAULT true NOT NULL,
    created_by uuid,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: user_presence; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.user_presence (
    user_id uuid NOT NULL,
    last_seen_at timestamp with time zone DEFAULT now() NOT NULL,
    session_started_at timestamp with time zone DEFAULT now() NOT NULL,
    total_seconds bigint DEFAULT 0 NOT NULL,
    today_seconds bigint DEFAULT 0 NOT NULL,
    day date DEFAULT CURRENT_DATE NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: user_roles; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.user_roles (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    user_id uuid NOT NULL,
    role public.app_role NOT NULL
);


--
-- Name: agent_settings agent_settings_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.agent_settings
    ADD CONSTRAINT agent_settings_pkey PRIMARY KEY (user_id);


--
-- Name: customers customers_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.customers
    ADD CONSTRAINT customers_pkey PRIMARY KEY (id);


--
-- Name: discount_codes discount_codes_code_key; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.discount_codes
    ADD CONSTRAINT discount_codes_code_key UNIQUE (code);


--
-- Name: discount_codes discount_codes_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.discount_codes
    ADD CONSTRAINT discount_codes_pkey PRIMARY KEY (id);


--
-- Name: installment_payments installment_payments_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.installment_payments
    ADD CONSTRAINT installment_payments_pkey PRIMARY KEY (id);


--
-- Name: installments installments_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.installments
    ADD CONSTRAINT installments_pkey PRIMARY KEY (id);


--
-- Name: invite_tokens invite_tokens_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.invite_tokens
    ADD CONSTRAINT invite_tokens_pkey PRIMARY KEY (id);


--
-- Name: invite_tokens invite_tokens_token_key; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.invite_tokens
    ADD CONSTRAINT invite_tokens_token_key UNIQUE (token);


--
-- Name: ledger_entries ledger_entries_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.ledger_entries
    ADD CONSTRAINT ledger_entries_pkey PRIMARY KEY (id);


--
-- Name: login_otps login_otps_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.login_otps
    ADD CONSTRAINT login_otps_pkey PRIMARY KEY (id);


--
-- Name: marketer_payouts marketer_payouts_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.marketer_payouts
    ADD CONSTRAINT marketer_payouts_pkey PRIMARY KEY (id);


--
-- Name: marketers marketers_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.marketers
    ADD CONSTRAINT marketers_pkey PRIMARY KEY (id);


--
-- Name: payment_gateways payment_gateways_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.payment_gateways
    ADD CONSTRAINT payment_gateways_pkey PRIMARY KEY (id);


--
-- Name: payments payments_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.payments
    ADD CONSTRAINT payments_pkey PRIMARY KEY (id);


--
-- Name: plans plans_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.plans
    ADD CONSTRAINT plans_pkey PRIMARY KEY (id);


--
-- Name: platform_ledger platform_ledger_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.platform_ledger
    ADD CONSTRAINT platform_ledger_pkey PRIMARY KEY (id);


--
-- Name: platform_payment platform_payment_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.platform_payment
    ADD CONSTRAINT platform_payment_pkey PRIMARY KEY (id);


--
-- Name: platform_sms platform_sms_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.platform_sms
    ADD CONSTRAINT platform_sms_pkey PRIMARY KEY (id);


--
-- Name: policies policies_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.policies
    ADD CONSTRAINT policies_pkey PRIMARY KEY (id);


--
-- Name: profiles profiles_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.profiles
    ADD CONSTRAINT profiles_pkey PRIMARY KEY (id);


--
-- Name: referrals referrals_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.referrals
    ADD CONSTRAINT referrals_pkey PRIMARY KEY (id);


--
-- Name: referrals referrals_referred_id_key; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.referrals
    ADD CONSTRAINT referrals_referred_id_key UNIQUE (referred_id);


--
-- Name: site_settings site_settings_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.site_settings
    ADD CONSTRAINT site_settings_pkey PRIMARY KEY (id);


--
-- Name: sms_logs sms_logs_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.sms_logs
    ADD CONSTRAINT sms_logs_pkey PRIMARY KEY (id);


--
-- Name: staff_members staff_members_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.staff_members
    ADD CONSTRAINT staff_members_pkey PRIMARY KEY (user_id);


--
-- Name: subscription_orders subscription_orders_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.subscription_orders
    ADD CONSTRAINT subscription_orders_pkey PRIMARY KEY (id);


--
-- Name: subscriptions subscriptions_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.subscriptions
    ADD CONSTRAINT subscriptions_pkey PRIMARY KEY (id);


--
-- Name: support_messages support_messages_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.support_messages
    ADD CONSTRAINT support_messages_pkey PRIMARY KEY (id);


--
-- Name: support_tickets support_tickets_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.support_tickets
    ADD CONSTRAINT support_tickets_pkey PRIMARY KEY (id);


--
-- Name: tutorials tutorials_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.tutorials
    ADD CONSTRAINT tutorials_pkey PRIMARY KEY (id);


--
-- Name: user_presence user_presence_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.user_presence
    ADD CONSTRAINT user_presence_pkey PRIMARY KEY (user_id);


--
-- Name: user_roles user_roles_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.user_roles
    ADD CONSTRAINT user_roles_pkey PRIMARY KEY (id);


--
-- Name: user_roles user_roles_user_id_role_key; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.user_roles
    ADD CONSTRAINT user_roles_user_id_role_key UNIQUE (user_id, role);


--
-- Name: idx_ledger_marketer; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_ledger_marketer ON public.ledger_entries USING btree (marketer_id);


--
-- Name: idx_policies_marketer; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_policies_marketer ON public.policies USING btree (marketer_id);


--
-- Name: installments_due_idx; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX installments_due_idx ON public.installments USING btree (due_date);


--
-- Name: login_otps_phone_idx; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX login_otps_phone_idx ON public.login_otps USING btree (phone, created_at DESC);


--
-- Name: platform_ledger_occurred_on_idx; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX platform_ledger_occurred_on_idx ON public.platform_ledger USING btree (occurred_on DESC);


--
-- Name: profiles_agency_code_unique_nonempty; Type: INDEX; Schema: public; Owner: -
--

CREATE UNIQUE INDEX profiles_agency_code_unique_nonempty ON public.profiles USING btree (agency_code) WHERE (agency_code <> ''::text);


--
-- Name: profiles_national_id_unique_nonempty; Type: INDEX; Schema: public; Owner: -
--

CREATE UNIQUE INDEX profiles_national_id_unique_nonempty ON public.profiles USING btree (national_id) WHERE (national_id <> ''::text);


--
-- Name: profiles_phone_unique_nonempty; Type: INDEX; Schema: public; Owner: -
--

CREATE UNIQUE INDEX profiles_phone_unique_nonempty ON public.profiles USING btree (phone) WHERE (phone <> ''::text);


--
-- Name: profiles_referral_code_key; Type: INDEX; Schema: public; Owner: -
--

CREATE UNIQUE INDEX profiles_referral_code_key ON public.profiles USING btree (referral_code) WHERE (referral_code <> ''::text);


--
-- Name: support_messages_ticket_idx; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX support_messages_ticket_idx ON public.support_messages USING btree (ticket_id, created_at);


--
-- Name: support_tickets_owner_idx; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX support_tickets_owner_idx ON public.support_tickets USING btree (owner_id, last_message_at DESC);


--
-- Name: support_messages support_messages_bump; Type: TRIGGER; Schema: public; Owner: -
--

CREATE TRIGGER support_messages_bump AFTER INSERT ON public.support_messages FOR EACH ROW EXECUTE FUNCTION public.bump_support_ticket();


--
-- Name: discount_codes update_discount_codes_updated_at; Type: TRIGGER; Schema: public; Owner: -
--

CREATE TRIGGER update_discount_codes_updated_at BEFORE UPDATE ON public.discount_codes FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();


--
-- Name: installment_payments update_installment_payments_updated_at; Type: TRIGGER; Schema: public; Owner: -
--

CREATE TRIGGER update_installment_payments_updated_at BEFORE UPDATE ON public.installment_payments FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();


--
-- Name: ledger_entries update_ledger_entries_updated_at; Type: TRIGGER; Schema: public; Owner: -
--

CREATE TRIGGER update_ledger_entries_updated_at BEFORE UPDATE ON public.ledger_entries FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();


--
-- Name: login_otps update_login_otps_updated_at; Type: TRIGGER; Schema: public; Owner: -
--

CREATE TRIGGER update_login_otps_updated_at BEFORE UPDATE ON public.login_otps FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();


--
-- Name: marketer_payouts update_marketer_payouts_updated_at; Type: TRIGGER; Schema: public; Owner: -
--

CREATE TRIGGER update_marketer_payouts_updated_at BEFORE UPDATE ON public.marketer_payouts FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();


--
-- Name: marketers update_marketers_updated_at; Type: TRIGGER; Schema: public; Owner: -
--

CREATE TRIGGER update_marketers_updated_at BEFORE UPDATE ON public.marketers FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();


--
-- Name: payment_gateways update_payment_gateways_updated_at; Type: TRIGGER; Schema: public; Owner: -
--

CREATE TRIGGER update_payment_gateways_updated_at BEFORE UPDATE ON public.payment_gateways FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();


--
-- Name: plans update_plans_updated_at; Type: TRIGGER; Schema: public; Owner: -
--

CREATE TRIGGER update_plans_updated_at BEFORE UPDATE ON public.plans FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();


--
-- Name: platform_ledger update_platform_ledger_updated_at; Type: TRIGGER; Schema: public; Owner: -
--

CREATE TRIGGER update_platform_ledger_updated_at BEFORE UPDATE ON public.platform_ledger FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();


--
-- Name: platform_payment update_platform_payment_updated_at; Type: TRIGGER; Schema: public; Owner: -
--

CREATE TRIGGER update_platform_payment_updated_at BEFORE UPDATE ON public.platform_payment FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();


--
-- Name: platform_sms update_platform_sms_updated_at; Type: TRIGGER; Schema: public; Owner: -
--

CREATE TRIGGER update_platform_sms_updated_at BEFORE UPDATE ON public.platform_sms FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();


--
-- Name: site_settings update_site_settings_updated_at; Type: TRIGGER; Schema: public; Owner: -
--

CREATE TRIGGER update_site_settings_updated_at BEFORE UPDATE ON public.site_settings FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();


--
-- Name: staff_members update_staff_members_updated_at; Type: TRIGGER; Schema: public; Owner: -
--

CREATE TRIGGER update_staff_members_updated_at BEFORE UPDATE ON public.staff_members FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();


--
-- Name: subscription_orders update_subscription_orders_updated_at; Type: TRIGGER; Schema: public; Owner: -
--

CREATE TRIGGER update_subscription_orders_updated_at BEFORE UPDATE ON public.subscription_orders FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();


--
-- Name: support_tickets update_support_tickets_updated_at; Type: TRIGGER; Schema: public; Owner: -
--

CREATE TRIGGER update_support_tickets_updated_at BEFORE UPDATE ON public.support_tickets FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();


--
-- Name: tutorials update_tutorials_updated_at; Type: TRIGGER; Schema: public; Owner: -
--

CREATE TRIGGER update_tutorials_updated_at BEFORE UPDATE ON public.tutorials FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();


--
-- Name: user_presence update_user_presence_updated_at; Type: TRIGGER; Schema: public; Owner: -
--

CREATE TRIGGER update_user_presence_updated_at BEFORE UPDATE ON public.user_presence FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();


--
-- Name: agent_settings agent_settings_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.agent_settings
    ADD CONSTRAINT agent_settings_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: customers customers_owner_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.customers
    ADD CONSTRAINT customers_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: discount_codes discount_codes_created_by_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.discount_codes
    ADD CONSTRAINT discount_codes_created_by_fkey FOREIGN KEY (created_by) REFERENCES auth.users(id);


--
-- Name: discount_codes discount_codes_plan_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.discount_codes
    ADD CONSTRAINT discount_codes_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE SET NULL;


--
-- Name: installment_payments installment_payments_customer_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.installment_payments
    ADD CONSTRAINT installment_payments_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES public.customers(id) ON DELETE CASCADE;


--
-- Name: installment_payments installment_payments_installment_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.installment_payments
    ADD CONSTRAINT installment_payments_installment_id_fkey FOREIGN KEY (installment_id) REFERENCES public.installments(id) ON DELETE CASCADE;


--
-- Name: installment_payments installment_payments_owner_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.installment_payments
    ADD CONSTRAINT installment_payments_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: installment_payments installment_payments_policy_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.installment_payments
    ADD CONSTRAINT installment_payments_policy_id_fkey FOREIGN KEY (policy_id) REFERENCES public.policies(id) ON DELETE CASCADE;


--
-- Name: installments installments_customer_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.installments
    ADD CONSTRAINT installments_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES public.customers(id) ON DELETE CASCADE;


--
-- Name: installments installments_owner_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.installments
    ADD CONSTRAINT installments_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: installments installments_policy_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.installments
    ADD CONSTRAINT installments_policy_id_fkey FOREIGN KEY (policy_id) REFERENCES public.policies(id) ON DELETE CASCADE;


--
-- Name: invite_tokens invite_tokens_created_by_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.invite_tokens
    ADD CONSTRAINT invite_tokens_created_by_fkey FOREIGN KEY (created_by) REFERENCES auth.users(id) ON DELETE SET NULL;


--
-- Name: invite_tokens invite_tokens_used_by_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.invite_tokens
    ADD CONSTRAINT invite_tokens_used_by_fkey FOREIGN KEY (used_by) REFERENCES auth.users(id) ON DELETE SET NULL;


--
-- Name: ledger_entries ledger_entries_customer_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.ledger_entries
    ADD CONSTRAINT ledger_entries_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES public.customers(id) ON DELETE SET NULL;


--
-- Name: ledger_entries ledger_entries_marketer_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.ledger_entries
    ADD CONSTRAINT ledger_entries_marketer_id_fkey FOREIGN KEY (marketer_id) REFERENCES public.marketers(id) ON DELETE SET NULL;


--
-- Name: ledger_entries ledger_entries_owner_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.ledger_entries
    ADD CONSTRAINT ledger_entries_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: ledger_entries ledger_entries_policy_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.ledger_entries
    ADD CONSTRAINT ledger_entries_policy_id_fkey FOREIGN KEY (policy_id) REFERENCES public.policies(id) ON DELETE SET NULL;


--
-- Name: marketer_payouts marketer_payouts_marketer_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.marketer_payouts
    ADD CONSTRAINT marketer_payouts_marketer_id_fkey FOREIGN KEY (marketer_id) REFERENCES public.marketers(id) ON DELETE CASCADE;


--
-- Name: marketer_payouts marketer_payouts_owner_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.marketer_payouts
    ADD CONSTRAINT marketer_payouts_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: marketers marketers_owner_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.marketers
    ADD CONSTRAINT marketers_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: payments payments_reviewed_by_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.payments
    ADD CONSTRAINT payments_reviewed_by_fkey FOREIGN KEY (reviewed_by) REFERENCES auth.users(id) ON DELETE SET NULL;


--
-- Name: payments payments_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.payments
    ADD CONSTRAINT payments_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: platform_ledger platform_ledger_created_by_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.platform_ledger
    ADD CONSTRAINT platform_ledger_created_by_fkey FOREIGN KEY (created_by) REFERENCES auth.users(id) ON DELETE SET NULL;


--
-- Name: policies policies_customer_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.policies
    ADD CONSTRAINT policies_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES public.customers(id) ON DELETE CASCADE;


--
-- Name: policies policies_marketer_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.policies
    ADD CONSTRAINT policies_marketer_id_fkey FOREIGN KEY (marketer_id) REFERENCES public.marketers(id) ON DELETE SET NULL;


--
-- Name: policies policies_owner_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.policies
    ADD CONSTRAINT policies_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: profiles profiles_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.profiles
    ADD CONSTRAINT profiles_id_fkey FOREIGN KEY (id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: referrals referrals_referred_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.referrals
    ADD CONSTRAINT referrals_referred_id_fkey FOREIGN KEY (referred_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: referrals referrals_referrer_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.referrals
    ADD CONSTRAINT referrals_referrer_id_fkey FOREIGN KEY (referrer_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: sms_logs sms_logs_customer_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.sms_logs
    ADD CONSTRAINT sms_logs_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES public.customers(id) ON DELETE SET NULL;


--
-- Name: sms_logs sms_logs_installment_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.sms_logs
    ADD CONSTRAINT sms_logs_installment_id_fkey FOREIGN KEY (installment_id) REFERENCES public.installments(id) ON DELETE SET NULL;


--
-- Name: sms_logs sms_logs_owner_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.sms_logs
    ADD CONSTRAINT sms_logs_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: staff_members staff_members_created_by_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.staff_members
    ADD CONSTRAINT staff_members_created_by_fkey FOREIGN KEY (created_by) REFERENCES auth.users(id);


--
-- Name: staff_members staff_members_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.staff_members
    ADD CONSTRAINT staff_members_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: subscriptions subscriptions_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.subscriptions
    ADD CONSTRAINT subscriptions_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: support_messages support_messages_sender_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.support_messages
    ADD CONSTRAINT support_messages_sender_id_fkey FOREIGN KEY (sender_id) REFERENCES auth.users(id) ON DELETE SET NULL;


--
-- Name: support_messages support_messages_ticket_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.support_messages
    ADD CONSTRAINT support_messages_ticket_id_fkey FOREIGN KEY (ticket_id) REFERENCES public.support_tickets(id) ON DELETE CASCADE;


--
-- Name: support_tickets support_tickets_owner_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.support_tickets
    ADD CONSTRAINT support_tickets_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: tutorials tutorials_created_by_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.tutorials
    ADD CONSTRAINT tutorials_created_by_fkey FOREIGN KEY (created_by) REFERENCES auth.users(id) ON DELETE SET NULL;


--
-- Name: user_presence user_presence_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.user_presence
    ADD CONSTRAINT user_presence_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: user_roles user_roles_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.user_roles
    ADD CONSTRAINT user_roles_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE;


--
-- Name: platform_payment Admins can read payment settings; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "Admins can read payment settings" ON public.platform_payment FOR SELECT TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role));


--
-- Name: payment_gateways Admins manage payment gateways; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "Admins manage payment gateways" ON public.payment_gateways TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role)) WITH CHECK (public.has_role(auth.uid(), 'admin'::public.app_role));


--
-- Name: platform_ledger Admins manage platform ledger; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "Admins manage platform ledger" ON public.platform_ledger TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role)) WITH CHECK (public.has_role(auth.uid(), 'admin'::public.app_role));


--
-- Name: staff_members admin manage staff; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "admin manage staff" ON public.staff_members TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role)) WITH CHECK (public.has_role(auth.uid(), 'admin'::public.app_role));


--
-- Name: invite_tokens admin manages invites; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "admin manages invites" ON public.invite_tokens TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role)) WITH CHECK (public.has_role(auth.uid(), 'admin'::public.app_role));


--
-- Name: user_presence admins read all presence; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "admins read all presence" ON public.user_presence FOR SELECT TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role));


--
-- Name: agent_settings; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.agent_settings ENABLE ROW LEVEL SECURITY;

--
-- Name: customers; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.customers ENABLE ROW LEVEL SECURITY;

--
-- Name: customers customers own; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "customers own" ON public.customers TO authenticated USING ((owner_id = auth.uid())) WITH CHECK ((owner_id = auth.uid()));


--
-- Name: discount_codes discount admin all; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "discount admin all" ON public.discount_codes TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role)) WITH CHECK (public.has_role(auth.uid(), 'admin'::public.app_role));


--
-- Name: discount_codes; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.discount_codes ENABLE ROW LEVEL SECURITY;

--
-- Name: installment_payments installment payments own; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "installment payments own" ON public.installment_payments TO authenticated USING ((owner_id = auth.uid())) WITH CHECK ((owner_id = auth.uid()));


--
-- Name: installment_payments; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.installment_payments ENABLE ROW LEVEL SECURITY;

--
-- Name: installments; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.installments ENABLE ROW LEVEL SECURITY;

--
-- Name: installments installments own; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "installments own" ON public.installments TO authenticated USING ((owner_id = auth.uid())) WITH CHECK ((owner_id = auth.uid()));


--
-- Name: invite_tokens; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.invite_tokens ENABLE ROW LEVEL SECURITY;

--
-- Name: ledger_entries ledger own; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "ledger own" ON public.ledger_entries TO authenticated USING ((owner_id = auth.uid())) WITH CHECK ((owner_id = auth.uid()));


--
-- Name: ledger_entries; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.ledger_entries ENABLE ROW LEVEL SECURITY;

--
-- Name: login_otps; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.login_otps ENABLE ROW LEVEL SECURITY;

--
-- Name: marketer_payouts; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.marketer_payouts ENABLE ROW LEVEL SECURITY;

--
-- Name: marketers; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.marketers ENABLE ROW LEVEL SECURITY;

--
-- Name: support_messages messages delete admin; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "messages delete admin" ON public.support_messages FOR DELETE TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role));


--
-- Name: support_messages messages insert; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "messages insert" ON public.support_messages FOR INSERT TO authenticated WITH CHECK (((sender_id = auth.uid()) AND (public.is_support_staff(auth.uid()) OR ((from_admin = false) AND (EXISTS ( SELECT 1
   FROM public.support_tickets t
  WHERE ((t.id = support_messages.ticket_id) AND (t.owner_id = auth.uid()))))))));


--
-- Name: support_messages messages select; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "messages select" ON public.support_messages FOR SELECT TO authenticated USING ((public.is_support_staff(auth.uid()) OR (EXISTS ( SELECT 1
   FROM public.support_tickets t
  WHERE ((t.id = support_messages.ticket_id) AND (t.owner_id = auth.uid()))))));


--
-- Name: profiles own profile insert; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "own profile insert" ON public.profiles FOR INSERT TO authenticated WITH CHECK ((id = auth.uid()));


--
-- Name: profiles own profile read; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "own profile read" ON public.profiles FOR SELECT TO authenticated USING (((id = auth.uid()) OR public.has_role(auth.uid(), 'admin'::public.app_role)));


--
-- Name: profiles own profile write; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "own profile write" ON public.profiles FOR UPDATE TO authenticated USING (((id = auth.uid()) OR public.has_role(auth.uid(), 'admin'::public.app_role))) WITH CHECK (((id = auth.uid()) OR public.has_role(auth.uid(), 'admin'::public.app_role)));


--
-- Name: support_tickets own tickets insert; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "own tickets insert" ON public.support_tickets FOR INSERT TO authenticated WITH CHECK ((owner_id = auth.uid()));


--
-- Name: support_tickets own tickets select; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "own tickets select" ON public.support_tickets FOR SELECT TO authenticated USING (((owner_id = auth.uid()) OR public.is_support_staff(auth.uid())));


--
-- Name: marketer_payouts owners manage own marketer payouts; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "owners manage own marketer payouts" ON public.marketer_payouts TO authenticated USING ((auth.uid() = owner_id)) WITH CHECK ((auth.uid() = owner_id));


--
-- Name: marketers owners manage own marketers; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "owners manage own marketers" ON public.marketers TO authenticated USING ((auth.uid() = owner_id)) WITH CHECK ((auth.uid() = owner_id));


--
-- Name: payments pay admin write; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "pay admin write" ON public.payments TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role)) WITH CHECK (public.has_role(auth.uid(), 'admin'::public.app_role));


--
-- Name: payments pay insert own; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "pay insert own" ON public.payments FOR INSERT TO authenticated WITH CHECK ((user_id = auth.uid()));


--
-- Name: payments pay read; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "pay read" ON public.payments FOR SELECT TO authenticated USING (((user_id = auth.uid()) OR public.has_role(auth.uid(), 'admin'::public.app_role)));


--
-- Name: payment_gateways; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.payment_gateways ENABLE ROW LEVEL SECURITY;

--
-- Name: payments; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.payments ENABLE ROW LEVEL SECURITY;

--
-- Name: plans; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.plans ENABLE ROW LEVEL SECURITY;

--
-- Name: plans plans admin all; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "plans admin all" ON public.plans TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role)) WITH CHECK (public.has_role(auth.uid(), 'admin'::public.app_role));


--
-- Name: plans plans public read; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "plans public read" ON public.plans FOR SELECT TO authenticated, anon USING ((active = true));


--
-- Name: platform_sms platform sms admin all; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "platform sms admin all" ON public.platform_sms TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role)) WITH CHECK (public.has_role(auth.uid(), 'admin'::public.app_role));


--
-- Name: platform_ledger; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.platform_ledger ENABLE ROW LEVEL SECURITY;

--
-- Name: platform_payment; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.platform_payment ENABLE ROW LEVEL SECURITY;

--
-- Name: platform_sms; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.platform_sms ENABLE ROW LEVEL SECURITY;

--
-- Name: policies; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.policies ENABLE ROW LEVEL SECURITY;

--
-- Name: policies policies own; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "policies own" ON public.policies TO authenticated USING ((owner_id = auth.uid())) WITH CHECK ((owner_id = auth.uid()));


--
-- Name: user_presence presence own row; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "presence own row" ON public.user_presence TO authenticated USING ((auth.uid() = user_id)) WITH CHECK ((auth.uid() = user_id));


--
-- Name: profiles; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.profiles ENABLE ROW LEVEL SECURITY;

--
-- Name: referrals; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.referrals ENABLE ROW LEVEL SECURITY;

--
-- Name: referrals referrals read own or admin; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "referrals read own or admin" ON public.referrals FOR SELECT TO authenticated USING (((referrer_id = auth.uid()) OR public.has_role(auth.uid(), 'admin'::public.app_role)));


--
-- Name: user_roles roles read; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "roles read" ON public.user_roles FOR SELECT TO authenticated USING (((user_id = auth.uid()) OR public.has_role(auth.uid(), 'admin'::public.app_role)));


--
-- Name: site_settings settings admin write; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "settings admin write" ON public.site_settings TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role)) WITH CHECK (public.has_role(auth.uid(), 'admin'::public.app_role));


--
-- Name: agent_settings settings own; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "settings own" ON public.agent_settings TO authenticated USING ((user_id = auth.uid())) WITH CHECK ((user_id = auth.uid()));


--
-- Name: site_settings settings public read; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "settings public read" ON public.site_settings FOR SELECT TO authenticated, anon USING (true);


--
-- Name: site_settings; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.site_settings ENABLE ROW LEVEL SECURITY;

--
-- Name: sms_logs sms own; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "sms own" ON public.sms_logs TO authenticated USING ((owner_id = auth.uid())) WITH CHECK ((owner_id = auth.uid()));


--
-- Name: sms_logs; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.sms_logs ENABLE ROW LEVEL SECURITY;

--
-- Name: staff_members staff read own or admin; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "staff read own or admin" ON public.staff_members FOR SELECT TO authenticated USING (((user_id = auth.uid()) OR public.has_role(auth.uid(), 'admin'::public.app_role)));


--
-- Name: staff_members; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.staff_members ENABLE ROW LEVEL SECURITY;

--
-- Name: subscriptions sub admin write; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "sub admin write" ON public.subscriptions TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role)) WITH CHECK (public.has_role(auth.uid(), 'admin'::public.app_role));


--
-- Name: subscriptions sub read; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "sub read" ON public.subscriptions FOR SELECT TO authenticated USING (((user_id = auth.uid()) OR public.has_role(auth.uid(), 'admin'::public.app_role)));


--
-- Name: subscription_orders; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.subscription_orders ENABLE ROW LEVEL SECURITY;

--
-- Name: subscriptions; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.subscriptions ENABLE ROW LEVEL SECURITY;

--
-- Name: profiles support staff read profiles; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "support staff read profiles" ON public.profiles FOR SELECT TO authenticated USING (public.is_support_staff(auth.uid()));


--
-- Name: support_messages; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.support_messages ENABLE ROW LEVEL SECURITY;

--
-- Name: support_tickets; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.support_tickets ENABLE ROW LEVEL SECURITY;

--
-- Name: support_tickets tickets delete admin; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "tickets delete admin" ON public.support_tickets FOR DELETE TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role));


--
-- Name: support_tickets tickets update; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "tickets update" ON public.support_tickets FOR UPDATE TO authenticated USING (((owner_id = auth.uid()) OR public.is_support_staff(auth.uid()))) WITH CHECK (((owner_id = auth.uid()) OR public.is_support_staff(auth.uid())));


--
-- Name: tutorials; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.tutorials ENABLE ROW LEVEL SECURITY;

--
-- Name: tutorials tutorials admin read all; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "tutorials admin read all" ON public.tutorials FOR SELECT TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role));


--
-- Name: tutorials tutorials admin write; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "tutorials admin write" ON public.tutorials TO authenticated USING (public.has_role(auth.uid(), 'admin'::public.app_role)) WITH CHECK (public.has_role(auth.uid(), 'admin'::public.app_role));


--
-- Name: tutorials tutorials public read; Type: POLICY; Schema: public; Owner: -
--

CREATE POLICY "tutorials public read" ON public.tutorials FOR SELECT TO authenticated, anon USING ((active = true));


--
-- Name: user_presence; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.user_presence ENABLE ROW LEVEL SECURITY;

--
-- Name: user_roles; Type: ROW SECURITY; Schema: public; Owner: -
--

ALTER TABLE public.user_roles ENABLE ROW LEVEL SECURITY;

--
-- PostgreSQL database dump complete
--

\unrestrict 5e1m4PNuOg4MdeK0WaimWUgWChhdkifajGmDuyLVHNdgxRokenI4nviT4z1dDjd

