-- 创建店铺套餐系列分配表
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 DEFAULT 0 NOT NULL,
    updater BIGINT DEFAULT 0 NOT NULL,
    shop_id BIGINT NOT NULL,
    series_id BIGINT NOT NULL,
    allocator_shop_id BIGINT NOT NULL,
    pricing_mode VARCHAR(20) NOT NULL,
    pricing_value BIGINT NOT NULL,
    one_time_commission_trigger VARCHAR(30),
    one_time_commission_threshold BIGINT DEFAULT 0,
    one_time_commission_amount BIGINT DEFAULT 0,
    status INT DEFAULT 1 NOT NULL
);

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（上级）';
COMMENT ON COLUMN tb_shop_series_allocation.pricing_mode IS '加价模式 fixed-固定金额 percent-百分比';
COMMENT ON COLUMN tb_shop_series_allocation.pricing_value IS '加价值（分或千分比）';
COMMENT ON COLUMN tb_shop_series_allocation.one_time_commission_trigger IS '一次性佣金触发类型 one_time_recharge-单次充值 accumulated_recharge-累计充值';
COMMENT ON COLUMN tb_shop_series_allocation.one_time_commission_threshold IS '一次性佣金触发阈值（分）';
COMMENT ON COLUMN tb_shop_series_allocation.one_time_commission_amount IS '一次性佣金金额（分）';
COMMENT ON COLUMN tb_shop_series_allocation.status IS '状态 1-启用 2-禁用';

-- 创建梯度佣金配置表
CREATE TABLE IF NOT EXISTS tb_shop_series_commission_tier (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    deleted_at TIMESTAMPTZ,
    creator BIGINT DEFAULT 0 NOT NULL,
    updater BIGINT DEFAULT 0 NOT NULL,
    allocation_id BIGINT NOT NULL,
    tier_type VARCHAR(20) NOT NULL,
    period_type VARCHAR(20) NOT NULL,
    period_start_date TIMESTAMPTZ,
    period_end_date TIMESTAMPTZ,
    threshold_value BIGINT NOT NULL,
    commission_amount BIGINT NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_shop_series_commission_tier_allocation_id ON tb_shop_series_commission_tier(allocation_id);

COMMENT ON TABLE tb_shop_series_commission_tier IS '梯度佣金配置表';
COMMENT ON COLUMN tb_shop_series_commission_tier.allocation_id IS '关联的分配ID';
COMMENT ON COLUMN tb_shop_series_commission_tier.tier_type IS '梯度类型 sales_count-销量 sales_amount-销售额';
COMMENT ON COLUMN tb_shop_series_commission_tier.period_type IS '周期类型 monthly-月度 quarterly-季度 yearly-年度 custom-自定义';
COMMENT ON COLUMN tb_shop_series_commission_tier.period_start_date IS '自定义周期开始日期';
COMMENT ON COLUMN tb_shop_series_commission_tier.period_end_date IS '自定义周期结束日期';
COMMENT ON COLUMN tb_shop_series_commission_tier.threshold_value IS '阈值（销量或金额分）';
COMMENT ON COLUMN tb_shop_series_commission_tier.commission_amount IS '佣金金额（分）';

-- 创建店铺单套餐分配表
CREATE TABLE IF NOT EXISTS tb_shop_package_allocation (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    deleted_at TIMESTAMPTZ,
    creator BIGINT DEFAULT 0 NOT NULL,
    updater BIGINT DEFAULT 0 NOT NULL,
    shop_id BIGINT NOT NULL,
    package_id BIGINT NOT NULL,
    allocation_id BIGINT NOT NULL,
    cost_price BIGINT NOT NULL,
    status INT DEFAULT 1 NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_shop_package_allocation_shop_id ON tb_shop_package_allocation(shop_id);
CREATE INDEX IF NOT EXISTS idx_shop_package_allocation_package_id ON tb_shop_package_allocation(package_id);
CREATE INDEX IF NOT EXISTS idx_shop_package_allocation_allocation_id ON tb_shop_package_allocation(allocation_id);
CREATE UNIQUE INDEX IF NOT EXISTS idx_shop_package_allocation_shop_package ON tb_shop_package_allocation(shop_id, package_id) WHERE deleted_at IS NULL;

COMMENT ON TABLE tb_shop_package_allocation IS '店铺单套餐分配表';
COMMENT ON COLUMN tb_shop_package_allocation.shop_id IS '被分配的店铺ID';
COMMENT ON COLUMN tb_shop_package_allocation.package_id IS '套餐ID';
COMMENT ON COLUMN tb_shop_package_allocation.allocation_id IS '关联的系列分配ID';
COMMENT ON COLUMN tb_shop_package_allocation.cost_price IS '覆盖的成本价（分）';
COMMENT ON COLUMN tb_shop_package_allocation.status IS '状态 1-启用 2-禁用';
