-- 重构一次性佣金分配：恢复系列分配表，移除套餐分配中的佣金字段
-- 原因：一次性佣金是系列级概念，不应存储在套餐分配中

-- ============================================
-- 1. 创建 tb_shop_series_allocation 表
-- ============================================

CREATE TABLE IF NOT EXISTS tb_shop_series_allocation (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    deleted_at TIMESTAMPTZ,
    creator BIGINT NOT NULL DEFAULT 0,
    updater BIGINT NOT NULL DEFAULT 0,
    shop_id BIGINT NOT NULL,
    series_id BIGINT NOT NULL,
    allocator_shop_id BIGINT NOT NULL DEFAULT 0,
    one_time_commission_amount BIGINT NOT NULL DEFAULT 0,
    enable_one_time_commission BOOLEAN NOT NULL DEFAULT false,
    one_time_commission_trigger VARCHAR(50),
    one_time_commission_threshold BIGINT NOT NULL DEFAULT 0,
    enable_force_recharge BOOLEAN NOT NULL DEFAULT false,
    force_recharge_amount BIGINT NOT NULL DEFAULT 0,
    force_recharge_trigger_type INT NOT NULL DEFAULT 2,
    status INT NOT NULL DEFAULT 1
);

-- 索引
CREATE INDEX IF NOT EXISTS idx_shop_series_allocation_shop_id 
    ON tb_shop_series_allocation(shop_id);
CREATE INDEX IF NOT EXISTS idx_shop_series_allocation_series_id 
    ON tb_shop_series_allocation(series_id);
CREATE INDEX IF NOT EXISTS idx_shop_series_allocation_allocator_shop_id 
    ON tb_shop_series_allocation(allocator_shop_id);
CREATE UNIQUE INDEX IF NOT EXISTS idx_shop_series_allocation_shop_series 
    ON tb_shop_series_allocation(shop_id, series_id) WHERE deleted_at IS NULL;

-- 注释
COMMENT ON TABLE tb_shop_series_allocation IS '店铺系列分配表 - 管理系列级一次性佣金配置';
COMMENT ON COLUMN tb_shop_series_allocation.shop_id IS '被分配店铺ID';
COMMENT ON COLUMN tb_shop_series_allocation.series_id IS '套餐系列ID';
COMMENT ON COLUMN tb_shop_series_allocation.allocator_shop_id IS '分配者店铺ID（0表示平台分配）';
COMMENT ON COLUMN tb_shop_series_allocation.one_time_commission_amount IS '该代理能拿的一次性佣金金额上限（分）';
COMMENT ON COLUMN tb_shop_series_allocation.enable_one_time_commission IS '是否启用一次性佣金';
COMMENT ON COLUMN tb_shop_series_allocation.one_time_commission_trigger IS '一次性佣金触发类型 first_recharge-首次充值 accumulated_recharge-累计充值';
COMMENT ON COLUMN tb_shop_series_allocation.one_time_commission_threshold IS '一次性佣金触发阈值（分）';
COMMENT ON COLUMN tb_shop_series_allocation.enable_force_recharge IS '是否启用强制充值';
COMMENT ON COLUMN tb_shop_series_allocation.force_recharge_amount IS '强制充值金额（分）';
COMMENT ON COLUMN tb_shop_series_allocation.force_recharge_trigger_type IS '强充触发类型 1-单次充值 2-累计充值';
COMMENT ON COLUMN tb_shop_series_allocation.status IS '状态 1-启用 2-禁用';

-- ============================================
-- 2. 修改 tb_shop_package_allocation 表
-- ============================================

-- 添加 series_allocation_id 字段（如果不存在）
ALTER TABLE tb_shop_package_allocation
ADD COLUMN IF NOT EXISTS series_allocation_id BIGINT;

COMMENT ON COLUMN tb_shop_package_allocation.series_allocation_id IS '关联的系列分配ID';

CREATE INDEX IF NOT EXISTS idx_shop_package_allocation_series_allocation_id 
    ON tb_shop_package_allocation(series_allocation_id);

-- 删除 one_time_commission_amount 字段（佣金配置移到系列分配）
ALTER TABLE tb_shop_package_allocation
DROP COLUMN IF EXISTS one_time_commission_amount;

-- 删除 series_id 字段（通过 series_allocation_id 关联）
ALTER TABLE tb_shop_package_allocation
DROP COLUMN IF EXISTS series_id;

-- ============================================
-- 3. 删除 tb_shop_series_one_time_commission_tier 表（未使用）
-- ============================================

DROP TABLE IF EXISTS tb_shop_series_one_time_commission_tier;
