-- Tables: Addons / Créditos
-- Gerado automaticamente em 2026-05-11T16:53:50.927Z
-- Total: 7

CREATE TABLE public.addon_credits (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    tenant_id uuid NOT NULL,
    owner_id uuid,
    addon_type text NOT NULL,
    balance integer DEFAULT 0 NOT NULL,
    total_purchased integer DEFAULT 0 NOT NULL,
    total_consumed integer DEFAULT 0 NOT NULL,
    low_balance_threshold integer DEFAULT 10,
    low_balance_notified boolean DEFAULT false,
    daily_limit integer,
    hourly_limit integer,
    daily_used integer DEFAULT 0,
    hourly_used integer DEFAULT 0,
    daily_reset_at timestamp with time zone,
    hourly_reset_at timestamp with time zone,
    from_number_override text,
    expires_at timestamp with time zone,
    is_active boolean DEFAULT true,
    created_at timestamp with time zone DEFAULT now(),
    updated_at timestamp with time zone DEFAULT now(),
    CONSTRAINT addon_credits_balance_nonneg_chk CHECK ((balance >= 0)),
    CONSTRAINT addon_credits_consumed_nonneg_chk CHECK ((total_consumed >= 0)),
    CONSTRAINT addon_credits_purchased_nonneg_chk CHECK ((total_purchased >= 0))
);

CREATE TABLE public.addon_products (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    slug text NOT NULL,
    name text NOT NULL,
    description text,
    addon_type text NOT NULL,
    icon text DEFAULT 'pi pi-box'::text,
    credits_amount integer DEFAULT 0,
    price_cents integer DEFAULT 0 NOT NULL,
    currency text DEFAULT 'BRL'::text,
    is_active boolean DEFAULT true,
    is_visible boolean DEFAULT true,
    sort_order integer DEFAULT 0,
    metadata jsonb DEFAULT '{}'::jsonb,
    created_at timestamp with time zone DEFAULT now(),
    updated_at timestamp with time zone DEFAULT now(),
    deleted_at timestamp with time zone
);

CREATE TABLE public.addon_transactions (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    tenant_id uuid NOT NULL,
    owner_id uuid,
    addon_type text NOT NULL,
    type text NOT NULL,
    amount integer NOT NULL,
    balance_before integer DEFAULT 0 NOT NULL,
    balance_after integer DEFAULT 0 NOT NULL,
    product_id uuid,
    queue_id uuid,
    description text,
    admin_user_id uuid,
    payment_method text,
    payment_reference text,
    price_cents integer,
    currency text DEFAULT 'BRL'::text,
    created_at timestamp with time zone DEFAULT now(),
    metadata jsonb DEFAULT '{}'::jsonb
);

CREATE TABLE public.whatsapp_credit_packages (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    name text NOT NULL,
    description text,
    credits integer NOT NULL,
    price_brl numeric(10,2) NOT NULL,
    is_active boolean DEFAULT true NOT NULL,
    is_featured boolean DEFAULT false NOT NULL,
    "position" integer DEFAULT 100 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT whatsapp_credit_packages_credits_check CHECK ((credits > 0)),
    CONSTRAINT whatsapp_credit_packages_name_check CHECK (((length(name) > 0) AND (length(name) <= 100))),
    CONSTRAINT whatsapp_credit_packages_price_brl_check CHECK ((price_brl > (0)::numeric))
);

CREATE TABLE public.whatsapp_credit_purchases (
    id uuid DEFAULT gen_random_uuid() NOT NULL,
    tenant_id uuid NOT NULL,
    package_id uuid,
    package_name text NOT NULL,
    credits integer NOT NULL,
    amount_brl numeric(10,2) NOT NULL,
    status text DEFAULT 'pending'::text NOT NULL,
    asaas_customer_id text,
    asaas_payment_id text,
    asaas_payment_link text,
    asaas_pix_qrcode text,
    asaas_pix_copy_paste text,
    paid_at timestamp with time zone,
    expires_at timestamp with time zone,
    failed_at timestamp with time zone,
    created_by uuid,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT whatsapp_credit_purchases_amount_brl_check CHECK ((amount_brl > (0)::numeric)),
    CONSTRAINT whatsapp_credit_purchases_credits_check CHECK ((credits > 0)),
    CONSTRAINT whatsapp_credit_purchases_status_check CHECK ((status = ANY (ARRAY['pending'::text, 'paid'::text, 'failed'::text, 'expired'::text, 'refunded'::text, 'cancelled'::text])))
);

CREATE TABLE public.whatsapp_credits_balance (
    tenant_id uuid NOT NULL,
    balance integer DEFAULT 0 NOT NULL,
    lifetime_purchased integer DEFAULT 0 NOT NULL,
    lifetime_used integer DEFAULT 0 NOT NULL,
    low_balance_threshold integer DEFAULT 20 NOT NULL,
    low_balance_alerted_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,
    CONSTRAINT whatsapp_credits_balance_balance_check CHECK ((balance >= 0)),
    CONSTRAINT whatsapp_credits_balance_low_balance_threshold_check CHECK ((low_balance_threshold >= 0))
);

CREATE TABLE public.whatsapp_credits_transactions (
    id bigint NOT NULL,
    tenant_id uuid NOT NULL,
    kind text NOT NULL,
    amount integer NOT NULL,
    balance_after integer NOT NULL,
    conversation_message_id bigint,
    purchase_id uuid,
    admin_id uuid,
    note text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    CONSTRAINT whatsapp_credits_transactions_kind_check CHECK ((kind = ANY (ARRAY['purchase'::text, 'usage'::text, 'topup_manual'::text, 'refund'::text, 'adjustment'::text])))
);
