-- 重构套餐分配和佣金系统
-- 1. 删除自动加价机制（pricing_mode, pricing_value）
-- 2. 重构返佣配置（新增 base_commission_mode, base_commission_value, enable_tier_commission）
-- 3. 修正梯度佣金逻辑（commission_tier 新增 commission_mode）
-- 4. 新增配置版本管理、成本价历史、统计缓存表

-- ============================================================
-- 1. 修改 tb_shop_series_allocation 表
-- ============================================================

-- 删除旧字段
ALTER TABLE tb_shop_series_allocation 
DROP COLUMN IF EXISTS pricing_mode,
DROP COLUMN IF EXISTS pricing_value,
DROP COLUMN IF EXISTS one_time_commission_trigger,
DROP COLUMN IF EXISTS one_time_commission_threshold,
DROP COLUMN IF EXISTS one_time_commission_amount;

-- 新增新字段
ALTER TABLE tb_shop_series_allocation 
ADD COLUMN base_commission_mode VARCHAR(20) NOT NULL DEFAULT 'percent',
ADD COLUMN base_commission_value BIGINT NOT NULL DEFAULT 0,
ADD COLUMN enable_tier_commission BOOLEAN NOT NULL DEFAULT FALSE;

-- 添加字段注释
COMMENT ON COLUMN tb_shop_series_allocation.base_commission_mode IS '基础返佣模式 fixed-固定金额 percent-百分比';
COMMENT ON COLUMN tb_shop_series_allocation.base_commission_value IS '基础返佣值（分或千分比，如200=20%）';
COMMENT ON COLUMN tb_shop_series_allocation.enable_tier_commission IS '是否启用梯度返佣';

-- ============================================================
-- 2. 修改 tb_shop_series_commission_tier 表
-- ============================================================

-- 新增 commission_mode 字段
ALTER TABLE tb_shop_series_commission_tier 
ADD COLUMN commission_mode VARCHAR(20) NOT NULL DEFAULT 'percent';

-- 添加字段注释
COMMENT ON COLUMN tb_shop_series_commission_tier.commission_mode IS '达标后返佣模式 fixed-固定金额 percent-百分比';

-- 删除旧字段 commission_amount，重命名为更语义化的 commission_value
ALTER TABLE tb_shop_series_commission_tier 
RENAME COLUMN commission_amount TO commission_value;

-- 更新字段注释
COMMENT ON COLUMN tb_shop_series_commission_tier.commission_value IS '达标后返佣值（分或千分比）';

-- ============================================================
-- 3. 创建 tb_shop_series_allocation_config 表（配置版本表）
-- ============================================================

CREATE TABLE IF NOT EXISTS tb_shop_series_allocation_config (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    deleted_at TIMESTAMPTZ,
    allocation_id BIGINT NOT NULL,
    version INT NOT NULL,
    base_commission_mode VARCHAR(20) NOT NULL,
    base_commission_value BIGINT NOT NULL,
    enable_tier_commission BOOLEAN NOT NULL,
    effective_from TIMESTAMPTZ NOT NULL,
    effective_to TIMESTAMPTZ
);

-- 创建索引
CREATE INDEX IF NOT EXISTS idx_allocation_config_allocation_id ON tb_shop_series_allocation_config(allocation_id);
CREATE INDEX IF NOT EXISTS idx_allocation_config_effective ON tb_shop_series_allocation_config(allocation_id, effective_to);

-- 添加表和字段注释
COMMENT ON TABLE tb_shop_series_allocation_config IS '套餐系列分配配置版本表';
COMMENT ON COLUMN tb_shop_series_allocation_config.allocation_id IS '关联的分配ID';
COMMENT ON COLUMN tb_shop_series_allocation_config.version IS '配置版本号';
COMMENT ON COLUMN tb_shop_series_allocation_config.base_commission_mode IS '基础返佣模式（配置快照）';
COMMENT ON COLUMN tb_shop_series_allocation_config.base_commission_value IS '基础返佣值（配置快照）';
COMMENT ON COLUMN tb_shop_series_allocation_config.enable_tier_commission IS '是否启用梯度返佣（配置快照）';
COMMENT ON COLUMN tb_shop_series_allocation_config.effective_from IS '生效开始时间';
COMMENT ON COLUMN tb_shop_series_allocation_config.effective_to IS '生效结束时间（NULL表示当前生效）';

-- ============================================================
-- 4. 创建 tb_shop_package_allocation_price_history 表（成本价历史表）
-- ============================================================

CREATE TABLE IF NOT EXISTS tb_shop_package_allocation_price_history (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    deleted_at TIMESTAMPTZ,
    allocation_id BIGINT NOT NULL,
    old_cost_price BIGINT NOT NULL,
    new_cost_price BIGINT NOT NULL,
    change_reason VARCHAR(255),
    changed_by BIGINT NOT NULL,
    effective_from TIMESTAMPTZ NOT NULL
);

-- 创建索引
CREATE INDEX IF NOT EXISTS idx_price_history_allocation ON tb_shop_package_allocation_price_history(allocation_id, effective_from);

-- 添加表和字段注释
COMMENT ON TABLE tb_shop_package_allocation_price_history IS '套餐成本价变更历史表';
COMMENT ON COLUMN tb_shop_package_allocation_price_history.allocation_id IS '关联的套餐分配ID（tb_shop_package_allocation.id）';
COMMENT ON COLUMN tb_shop_package_allocation_price_history.old_cost_price IS '原成本价（分）';
COMMENT ON COLUMN tb_shop_package_allocation_price_history.new_cost_price IS '新成本价（分）';
COMMENT ON COLUMN tb_shop_package_allocation_price_history.change_reason IS '变更原因';
COMMENT ON COLUMN tb_shop_package_allocation_price_history.changed_by IS '变更人ID';
COMMENT ON COLUMN tb_shop_package_allocation_price_history.effective_from IS '生效时间';

-- ============================================================
-- 5. 创建 tb_shop_series_commission_stats 表（统计缓存表）
-- ============================================================

CREATE TABLE IF NOT EXISTS tb_shop_series_commission_stats (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    deleted_at TIMESTAMPTZ,
    allocation_id BIGINT NOT NULL,
    period_type VARCHAR(20) NOT NULL,
    period_start TIMESTAMPTZ NOT NULL,
    period_end TIMESTAMPTZ NOT NULL,
    total_sales_count BIGINT DEFAULT 0 NOT NULL,
    total_sales_amount BIGINT DEFAULT 0 NOT NULL,
    current_tier_id BIGINT,
    last_updated_at TIMESTAMPTZ NOT NULL,
    version INT DEFAULT 0 NOT NULL,
    status VARCHAR(20) DEFAULT 'active' NOT NULL
);

-- 创建索引
CREATE INDEX IF NOT EXISTS idx_commission_stats_allocation_id ON tb_shop_series_commission_stats(allocation_id);
CREATE INDEX IF NOT EXISTS idx_commission_stats_period ON tb_shop_series_commission_stats(allocation_id, period_start, period_end);
CREATE UNIQUE INDEX IF NOT EXISTS idx_commission_stats_unique ON tb_shop_series_commission_stats(allocation_id, period_type, period_start) WHERE deleted_at IS NULL;

-- 添加表和字段注释
COMMENT ON TABLE tb_shop_series_commission_stats IS '梯度佣金统计缓存表';
COMMENT ON COLUMN tb_shop_series_commission_stats.allocation_id IS '关联的分配ID';
COMMENT ON COLUMN tb_shop_series_commission_stats.period_type IS '周期类型 monthly-月度 quarterly-季度 yearly-年度';
COMMENT ON COLUMN tb_shop_series_commission_stats.period_start IS '周期开始时间';
COMMENT ON COLUMN tb_shop_series_commission_stats.period_end IS '周期结束时间';
COMMENT ON COLUMN tb_shop_series_commission_stats.total_sales_count IS '总销售数量';
COMMENT ON COLUMN tb_shop_series_commission_stats.total_sales_amount IS '总销售金额（分）';
COMMENT ON COLUMN tb_shop_series_commission_stats.current_tier_id IS '当前匹配的梯度ID';
COMMENT ON COLUMN tb_shop_series_commission_stats.last_updated_at IS '最后更新时间';
COMMENT ON COLUMN tb_shop_series_commission_stats.version IS '版本号（乐观锁）';
COMMENT ON COLUMN tb_shop_series_commission_stats.status IS '状态 active-活跃 completed-已完成 cancelled-已取消';

-- ============================================================
-- 6. 创建额外索引（性能优化）
-- ============================================================

-- tb_shop_package_allocation 表新增复合索引（用于代理权限过滤）
CREATE INDEX IF NOT EXISTS idx_package_allocation_shop_pkg ON tb_shop_package_allocation(shop_id, package_id, status);
