-- 回滚: 删除 tb_package 表的新字段
ALTER TABLE tb_package DROP COLUMN IF EXISTS suggested_cost_price;
ALTER TABLE tb_package DROP COLUMN IF EXISTS suggested_retail_price;
ALTER TABLE tb_package DROP COLUMN IF EXISTS shelf_status;

-- 回滚: 重建废弃表结构（仅结构，不恢复数据）

-- 代理层级关系表
CREATE TABLE IF NOT EXISTS tb_agent_hierarchy (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    deleted_at TIMESTAMPTZ,
    creator BIGINT DEFAULT 0,
    updater BIGINT DEFAULT 0,
    agent_id BIGINT NOT NULL,
    parent_agent_id BIGINT,
    level INT NOT NULL,
    path VARCHAR(500)
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_agent_hierarchy_agent ON tb_agent_hierarchy(agent_id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_agent_hierarchy_parent ON tb_agent_hierarchy(parent_agent_id);
COMMENT ON TABLE tb_agent_hierarchy IS '代理层级关系表';

-- 分佣规则表
CREATE TABLE IF NOT EXISTS tb_commission_rule (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    deleted_at TIMESTAMPTZ,
    creator BIGINT DEFAULT 0,
    updater BIGINT DEFAULT 0,
    agent_id BIGINT NOT NULL,
    business_type VARCHAR(50) NOT NULL,
    card_type VARCHAR(50) NOT NULL,
    series_id BIGINT,
    package_id BIGINT,
    commission_type VARCHAR(50) NOT NULL,
    commission_mode VARCHAR(20) NOT NULL,
    commission_value BIGINT NOT NULL,
    unfreeze_days INT DEFAULT 0,
    min_activation_for_unfreeze INT DEFAULT 0,
    approval_type VARCHAR(20) DEFAULT 'auto',
    status INT DEFAULT 1 NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_commission_rule_agent ON tb_commission_rule(agent_id);
CREATE INDEX IF NOT EXISTS idx_commission_rule_series ON tb_commission_rule(series_id);
CREATE INDEX IF NOT EXISTS idx_commission_rule_package ON tb_commission_rule(package_id);
COMMENT ON TABLE tb_commission_rule IS '分佣规则表';

-- 阶梯分佣配置表
CREATE TABLE IF NOT EXISTS tb_commission_ladder (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    deleted_at TIMESTAMPTZ,
    creator BIGINT DEFAULT 0,
    updater BIGINT DEFAULT 0,
    rule_id BIGINT NOT NULL,
    ladder_type VARCHAR(50) NOT NULL,
    threshold_value INT NOT NULL,
    commission_mode VARCHAR(20) NOT NULL,
    commission_value BIGINT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_commission_ladder_rule ON tb_commission_ladder(rule_id);
COMMENT ON TABLE tb_commission_ladder IS '阶梯分佣配置表';

-- 组合分佣条件表
CREATE TABLE IF NOT EXISTS tb_commission_combined_condition (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    deleted_at TIMESTAMPTZ,
    creator BIGINT DEFAULT 0,
    updater BIGINT DEFAULT 0,
    rule_id BIGINT NOT NULL,
    one_time_commission_mode VARCHAR(20),
    one_time_commission_value BIGINT,
    long_term_commission_mode VARCHAR(20),
    long_term_commission_value BIGINT,
    long_term_trigger_time_point TIMESTAMPTZ,
    long_term_trigger_package_cycles INT,
    long_term_trigger_network_months INT,
    long_term_unfreeze_days INT DEFAULT 0,
    long_term_min_activation INT DEFAULT 0
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_commission_combined_rule ON tb_commission_combined_condition(rule_id) WHERE deleted_at IS NULL;
COMMENT ON TABLE tb_commission_combined_condition IS '组合分佣条件表';

-- 分佣审批表
CREATE TABLE IF NOT EXISTS tb_commission_approval (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    deleted_at TIMESTAMPTZ,
    creator BIGINT DEFAULT 0,
    updater BIGINT DEFAULT 0,
    commission_record_id BIGINT NOT NULL,
    approver_id BIGINT,
    status INT DEFAULT 1 NOT NULL,
    reason TEXT
);
CREATE INDEX IF NOT EXISTS idx_commission_approval_record ON tb_commission_approval(commission_record_id);
CREATE INDEX IF NOT EXISTS idx_commission_approval_approver ON tb_commission_approval(approver_id);
COMMENT ON TABLE tb_commission_approval IS '分佣审批表';

-- 分佣模板表
CREATE TABLE IF NOT EXISTS tb_commission_template (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    deleted_at TIMESTAMPTZ,
    creator BIGINT DEFAULT 0,
    updater BIGINT DEFAULT 0,
    template_name VARCHAR(255) NOT NULL,
    business_type VARCHAR(50) NOT NULL,
    card_type VARCHAR(50) NOT NULL,
    commission_type VARCHAR(50) NOT NULL,
    commission_mode VARCHAR(20) NOT NULL,
    commission_value BIGINT NOT NULL,
    unfreeze_days INT DEFAULT 0,
    min_activation_for_unfreeze INT DEFAULT 0,
    approval_type VARCHAR(20) DEFAULT 'auto'
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_commission_template_name ON tb_commission_template(template_name) WHERE deleted_at IS NULL;
COMMENT ON TABLE tb_commission_template IS '分佣模板表';

-- 运营商结算表
CREATE TABLE IF NOT EXISTS tb_carrier_settlement (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    deleted_at TIMESTAMPTZ,
    creator BIGINT DEFAULT 0,
    updater BIGINT DEFAULT 0,
    commission_record_id BIGINT NOT NULL,
    agent_id BIGINT NOT NULL,
    settlement_month VARCHAR(20) NOT NULL,
    settlement_amount BIGINT NOT NULL,
    status INT DEFAULT 1 NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_carrier_settlement_record ON tb_carrier_settlement(commission_record_id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_carrier_settlement_agent ON tb_carrier_settlement(agent_id);
COMMENT ON TABLE tb_carrier_settlement IS '运营商结算表';

-- 代理套餐分配表
CREATE TABLE IF NOT EXISTS tb_agent_package_allocation (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    deleted_at TIMESTAMPTZ,
    creator BIGINT DEFAULT 0,
    updater BIGINT DEFAULT 0,
    agent_id BIGINT NOT NULL,
    package_id BIGINT NOT NULL,
    cost_price BIGINT NOT NULL,
    retail_price BIGINT NOT NULL,
    status INT DEFAULT 1 NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_agent_package_allocation_agent ON tb_agent_package_allocation(agent_id);
CREATE INDEX IF NOT EXISTS idx_agent_package_allocation_package ON tb_agent_package_allocation(package_id);
COMMENT ON TABLE tb_agent_package_allocation IS '代理套餐分配表';
