-- =====================================================================================
-- Schema: 分销代理商管理表
-- 位置：docs/sql/10_schema/distribution/ak_distribution_agents_v1.sql
-- 对象类型：TABLE
-- 版本：v1
-- 依赖：ak_users, ak_distribution_divisions
-- =====================================================================================

CREATE TABLE IF NOT EXISTS public.ak_distribution_agents (
    uid UUID PRIMARY KEY REFERENCES public.ak_users(id) ON DELETE CASCADE,
    division_uid UUID NOT NULL REFERENCES public.ak_distribution_divisions(uid), -- 所属事业部
    name TEXT 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_agents_division_uid ON public.ak_distribution_agents(division_uid);

-- 注释
COMMENT ON TABLE public.ak_distribution_agents IS '分销代理商信息表';
COMMENT ON COLUMN public.ak_distribution_agents.uid IS '用户ID（关联代理商本人）';
COMMENT ON COLUMN public.ak_distribution_agents.division_uid IS '所属事业部UID';
COMMENT ON COLUMN public.ak_distribution_agents.commission_ratio IS '代理商固定分佣比例(%)';
