-- =====================================================================================
-- Schema: 分销事业部管理表
-- 位置：docs/sql/10_schema/distribution/ak_distribution_divisions_v1.sql
-- 对象类型：TABLE
-- 版本：v1
-- 依赖：ak_users
-- =====================================================================================

CREATE TABLE IF NOT EXISTS public.ak_distribution_divisions (
    uid UUID PRIMARY KEY REFERENCES public.ak_users(id) ON DELETE CASCADE,
    name TEXT NOT NULL,
    invite_code TEXT UNIQUE NOT NULL,
    commission_ratio NUMERIC(5,2) DEFAULT 0 CHECK (commission_ratio >= 0 AND commission_ratio <= 100),
    is_enabled BOOLEAN DEFAULT TRUE,
    end_time TIMESTAMPTZ,
    created_at TIMESTAMPTZ DEFAULT now(),
    updated_at TIMESTAMPTZ DEFAULT now(),
    created_by UUID REFERENCES public.ak_users(id),
    updated_by UUID REFERENCES public.ak_users(id)
);

-- 索引
CREATE INDEX IF NOT EXISTS idx_distribution_divisions_invite_code ON public.ak_distribution_divisions(invite_code);

-- 注释
COMMENT ON TABLE public.ak_distribution_divisions IS '分销事业部信息表';
COMMENT ON COLUMN public.ak_distribution_divisions.uid IS '用户ID（关联事业部负责人）';
COMMENT ON COLUMN public.ak_distribution_divisions.invite_code IS '事业部专属邀请码';
COMMENT ON COLUMN public.ak_distribution_divisions.commission_ratio IS '事业部固定分佣比例(%)';
COMMENT ON COLUMN public.ak_distribution_divisions.end_time IS '事业部有效截止时间';
