-- 套餐与佣金模型重构
-- 重构说明：
-- 1. Package 表：移除废弃字段，新增虚流量开关
-- 2. ShopPackageAllocation 表：新增一次性佣金金额字段
-- 3. IoTCard/Device 表：新增按系列追踪的累计充值和首充状态字段
-- 4. PackageSeries 表：新增一次性佣金规则配置字段
-- 5. ShopSeriesOneTimeCommissionTier 表：新增统计范围字段
-- 6. ShopSeriesAllocation 表：移除完整一次性佣金配置，改为只存金额

-- ============================================
-- 1. Package 表调整
-- ============================================

-- 移除废弃字段
ALTER TABLE tb_package 
DROP COLUMN IF EXISTS price,
DROP COLUMN IF EXISTS data_type,
DROP COLUMN IF EXISTS data_amount_mb;

-- 新增虚流量开关字段
ALTER TABLE tb_package
ADD COLUMN IF NOT EXISTS enable_virtual_data BOOLEAN DEFAULT false NOT NULL;

COMMENT ON COLUMN tb_package.enable_virtual_data IS '是否启用虚流量';

-- 重命名 suggested_cost_price 为 cost_price（如果存在的话，通过 RENAME 实现）
-- 注意：PostgreSQL 不支持条件性重命名，此处直接重命名
DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_name = 'tb_package' AND column_name = 'suggested_cost_price'
    ) THEN
        ALTER TABLE tb_package RENAME COLUMN suggested_cost_price TO cost_price;
    END IF;
END $$;

COMMENT ON COLUMN tb_package.cost_price IS '成本价（分为单位）';

-- ============================================
-- 2. ShopPackageAllocation 表新增字段
-- ============================================

ALTER TABLE tb_shop_package_allocation
ADD COLUMN IF NOT EXISTS one_time_commission_amount BIGINT DEFAULT 0 NOT NULL;

COMMENT ON COLUMN tb_shop_package_allocation.one_time_commission_amount IS '该代理能拿到的一次性佣金（分）';

-- ============================================
-- 3. IoTCard 表新增追踪字段
-- ============================================

-- 新增按系列追踪的累计充值字段（JSON Map: series_id -> amount）
ALTER TABLE tb_iot_card
ADD COLUMN IF NOT EXISTS accumulated_recharge_by_series JSONB DEFAULT '{}';

-- 新增按系列追踪的首充触发状态（JSON Map: series_id -> bool）
ALTER TABLE tb_iot_card
ADD COLUMN IF NOT EXISTS first_recharge_triggered_by_series JSONB DEFAULT '{}';

COMMENT ON COLUMN tb_iot_card.accumulated_recharge_by_series IS '按套餐系列追踪的累计充值金额（JSON Map: series_id -> amount 分）';
COMMENT ON COLUMN tb_iot_card.first_recharge_triggered_by_series IS '按套餐系列追踪的首充触发状态（JSON Map: series_id -> bool）';

-- ============================================
-- 4. Device 表新增追踪字段
-- ============================================

-- 新增按系列追踪的累计充值字段
ALTER TABLE tb_device
ADD COLUMN IF NOT EXISTS accumulated_recharge_by_series JSONB DEFAULT '{}';

-- 新增按系列追踪的首充触发状态
ALTER TABLE tb_device
ADD COLUMN IF NOT EXISTS first_recharge_triggered_by_series JSONB DEFAULT '{}';

COMMENT ON COLUMN tb_device.accumulated_recharge_by_series IS '按套餐系列追踪的累计充值金额（JSON Map: series_id -> amount 分）';
COMMENT ON COLUMN tb_device.first_recharge_triggered_by_series IS '按套餐系列追踪的首充触发状态（JSON Map: series_id -> bool）';

-- ============================================
-- 5. PackageSeries 表新增一次性佣金规则配置
-- ============================================

ALTER TABLE tb_package_series
ADD COLUMN IF NOT EXISTS one_time_commission_config JSONB;

COMMENT ON COLUMN tb_package_series.one_time_commission_config IS '一次性佣金规则配置（JSON：enable, trigger_type, threshold, commission_type, commission_amount, validity_type 等）';

-- ============================================
-- 6. ShopSeriesOneTimeCommissionTier 表新增统计范围
-- ============================================

-- 检查表是否存在，存在则添加字段
DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.tables 
        WHERE table_name = 'tb_shop_series_one_time_commission_tier'
    ) THEN
        ALTER TABLE tb_shop_series_one_time_commission_tier
        ADD COLUMN IF NOT EXISTS stat_scope VARCHAR(20) DEFAULT 'self' NOT NULL;
        
        COMMENT ON COLUMN tb_shop_series_one_time_commission_tier.stat_scope IS '统计范围 self-仅自己 self_and_sub-自己+下级';
    END IF;
END $$;

-- ============================================
-- 7. ShopSeriesAllocation 表移除废弃字段，新增新字段
-- ============================================

-- 移除一次性佣金完整配置字段
ALTER TABLE tb_shop_series_allocation
DROP COLUMN IF EXISTS enable_one_time_commission,
DROP COLUMN IF EXISTS one_time_commission_type,
DROP COLUMN IF EXISTS one_time_commission_trigger,
DROP COLUMN IF EXISTS one_time_commission_threshold,
DROP COLUMN IF EXISTS one_time_commission_mode,
DROP COLUMN IF EXISTS one_time_commission_value,
DROP COLUMN IF EXISTS enable_force_recharge,
DROP COLUMN IF EXISTS force_recharge_amount,
DROP COLUMN IF EXISTS force_recharge_trigger_type;

-- 新增一次性佣金金额字段
ALTER TABLE tb_shop_series_allocation
ADD COLUMN IF NOT EXISTS one_time_commission_amount BIGINT DEFAULT 0 NOT NULL;

COMMENT ON COLUMN tb_shop_series_allocation.one_time_commission_amount IS '给被分配店铺的一次性佣金金额（分）';
