-- 回滚：恢复原有结构

-- ============================================
-- 1. 恢复 tb_shop_package_allocation 字段
-- ============================================

-- 恢复 series_id 字段
ALTER TABLE tb_shop_package_allocation
ADD COLUMN IF NOT EXISTS series_id BIGINT NOT NULL DEFAULT 0;

COMMENT ON COLUMN tb_shop_package_allocation.series_id IS '套餐系列ID（冗余字段，便于查询）';

-- 恢复 one_time_commission_amount 字段
ALTER TABLE tb_shop_package_allocation
ADD COLUMN IF NOT EXISTS one_time_commission_amount BIGINT NOT NULL DEFAULT 0;

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

-- 删除 series_allocation_id 字段
DROP INDEX IF EXISTS idx_shop_package_allocation_series_allocation_id;

ALTER TABLE tb_shop_package_allocation
DROP COLUMN IF EXISTS series_allocation_id;

-- ============================================
-- 2. 恢复 tb_shop_series_one_time_commission_tier 表
-- ============================================

CREATE TABLE IF NOT EXISTS tb_shop_series_one_time_commission_tier (
    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,
    allocation_id BIGINT NOT NULL,
    tier_type VARCHAR(50) NOT NULL,
    threshold_value BIGINT NOT NULL DEFAULT 0,
    commission_mode VARCHAR(20) NOT NULL DEFAULT 'fixed',
    commission_value BIGINT NOT NULL DEFAULT 0,
    stat_scope VARCHAR(50) NOT NULL DEFAULT 'self',
    status INT NOT NULL DEFAULT 1
);

CREATE INDEX IF NOT EXISTS idx_shop_series_otc_tier_allocation_id 
    ON tb_shop_series_one_time_commission_tier(allocation_id);

COMMENT ON TABLE tb_shop_series_one_time_commission_tier IS '一次性佣金梯度配置表';

-- ============================================
-- 3. 删除 tb_shop_series_allocation 表
-- ============================================

DROP TABLE IF EXISTS tb_shop_series_allocation;
