-- V9: 添加缺失的经济系统和等级系统表 (2026-05-18)
-- 服务器数据库缺少以下表: user_mint_count, mint_cost_config, level_*, transaction_records, mint_*_config

BEGIN;

-- =====================================================
-- 1. user_mint_count 铸爱次数表 (V5原有)
-- =====================================================
CREATE TABLE IF NOT EXISTS public.user_mint_count (
    id BIGSERIAL PRIMARY KEY,
    user_id BIGINT NOT NULL,
    star_id BIGINT NOT NULL,
    mint_count INT NOT NULL DEFAULT 0,
    revenue_boost_bps INT NOT NULL DEFAULT 0,
    updated_at BIGINT NOT NULL,
    CONSTRAINT uk_user_mint_count_user_star UNIQUE (user_id, star_id)
);

CREATE INDEX IF NOT EXISTS idx_user_mint_count_user ON public.user_mint_count USING btree (user_id);
CREATE INDEX IF NOT EXISTS idx_user_mint_count_star ON public.user_mint_count USING btree (star_id);

COMMENT ON TABLE public.user_mint_count IS '用户在某偶像下的累计铸爱次数与收益加成（基点）';

-- =====================================================
-- 2. mint_cost_config 铸爱阶梯配置表 (V5原有)
-- =====================================================
CREATE TABLE IF NOT EXISTS public.mint_cost_config (
    id BIGSERIAL PRIMARY KEY,
    mint_count INT NOT NULL,
    cost_crystal BIGINT NOT NULL,
    probability BIGINT NOT NULL DEFAULT 0,
    reward_type VARCHAR(50) DEFAULT NULL,
    reward_value BIGINT NOT NULL DEFAULT 0,
    description VARCHAR(255),
    updated_at BIGINT NOT NULL,
    CONSTRAINT uk_mint_cost_config_mint_count UNIQUE (mint_count)
);

COMMENT ON TABLE public.mint_cost_config IS '铸爱次数阶梯消耗与保底配置';

-- 插入铸爱阶梯数据
INSERT INTO public.mint_cost_config (mint_count, cost_crystal, probability, reward_type, reward_value, description, updated_at)
VALUES
    (1, 2, 0, NULL, 0, '第1次', 1773322573872),
    (2, 4, 0, NULL, 0, '第2次', 1773322573872),
    (3, 8, 0, NULL, 0, '第3次', 1773322573872),
    (4, 16, 0, NULL, 0, '第4次', 1773322573872),
    (5, 32, 0, NULL, 0, '第5次', 1773322573872),
    (6, 64, 0, NULL, 0, '第6次', 1773322573872),
    (7, 128, 0, NULL, 0, '第7次', 1773322573872),
    (8, 256, 0, NULL, 0, '第8次', 1773322573872),
    (9, 512, 20, '收益提升', 5, '小保底', 1773322573872),
    (10, 1024, 100, '收益提升', 5, '大保底', 1773322573872)
ON CONFLICT (mint_count) DO UPDATE SET
    cost_crystal = EXCLUDED.cost_crystal,
    probability = EXCLUDED.probability,
    reward_type = EXCLUDED.reward_type,
    reward_value = EXCLUDED.reward_value,
    description = EXCLUDED.description,
    updated_at = EXCLUDED.updated_at;

-- =====================================================
-- 3. level_thresholds 等级阈值表
-- =====================================================
CREATE TABLE IF NOT EXISTS public.level_thresholds (
    id BIGSERIAL PRIMARY KEY,
    level INT NOT NULL,
    exp_requirement BIGINT NOT NULL DEFAULT 0,
    title VARCHAR(50),
    badge_url VARCHAR(500),
    privileges JSONB,
    updated_at BIGINT NOT NULL,
    CONSTRAINT uk_level_thresholds_level UNIQUE (level)
);

COMMENT ON TABLE public.level_thresholds IS '等级阈值配置表';

-- =====================================================
-- 4. level_up_reward_config 升级奖励配置表
-- =====================================================
CREATE TABLE IF NOT EXISTS public.level_up_reward_config (
    id BIGSERIAL PRIMARY KEY,
    level INT NOT NULL,
    reward_type VARCHAR(50) NOT NULL,
    reward_amount BIGINT NOT NULL DEFAULT 0,
    description VARCHAR(255),
    updated_at BIGINT NOT NULL,
    CONSTRAINT uk_level_up_reward_level_type UNIQUE (level, reward_type)
);

COMMENT ON TABLE public.level_up_reward_config IS '升级奖励配置表';

-- =====================================================
-- 5. level_upgrade_conditions 升级条件表
-- =====================================================
CREATE TABLE IF NOT EXISTS public.level_upgrade_conditions (
    id BIGSERIAL PRIMARY KEY,
    level INT NOT NULL,
    condition_type VARCHAR(50) NOT NULL,
    condition_value BIGINT NOT NULL DEFAULT 0,
    description VARCHAR(255),
    updated_at BIGINT NOT NULL,
    CONSTRAINT uk_level_upgrade_conditions_level_type UNIQUE (level, condition_type)
);

COMMENT ON TABLE public.level_upgrade_conditions IS '升级条件配置表';

-- =====================================================
-- 6. dazi_level_thresholds 靶子等级阈值表
-- =====================================================
CREATE TABLE IF NOT EXISTS public.dazi_level_thresholds (
    id BIGSERIAL PRIMARY KEY,
    level INT NOT NULL,
    exp_requirement BIGINT NOT NULL DEFAULT 0,
    title VARCHAR(50),
    updated_at BIGINT NOT NULL,
    CONSTRAINT uk_dazi_level_thresholds_level UNIQUE (level)
);

COMMENT ON TABLE public.dazi_level_thresholds IS '靶子等级阈值配置表';

-- =====================================================
-- 7. level_cap_config 等级上限配置表
-- =====================================================
CREATE TABLE IF NOT EXISTS public.level_cap_config (
    id BIGSERIAL PRIMARY KEY,
    star_id BIGINT NOT NULL,
    max_level INT NOT NULL DEFAULT 50,
    exp_multiplier DECIMAL(5,2) NOT NULL DEFAULT 1.0,
    updated_at BIGINT NOT NULL,
    CONSTRAINT uk_level_cap_config_star UNIQUE (star_id)
);

COMMENT ON TABLE public.level_cap_config IS '等级上限配置表';

-- =====================================================
-- 8. user_dazi_level 用户靶子等级表
-- =====================================================
CREATE TABLE IF NOT EXISTS public.user_dazi_level (
    id BIGSERIAL PRIMARY KEY,
    user_id BIGINT NOT NULL,
    star_id BIGINT NOT NULL,
    level INT NOT NULL DEFAULT 1,
    exp BIGINT NOT NULL DEFAULT 0,
    updated_at BIGINT NOT NULL,
    CONSTRAINT uk_user_dazi_level_user_star UNIQUE (user_id, star_id)
);

CREATE INDEX IF NOT EXISTS idx_user_dazi_level_user ON public.user_dazi_level USING btree (user_id);
CREATE INDEX IF NOT EXISTS idx_user_dazi_level_star ON public.user_dazi_level USING btree (star_id);

COMMENT ON TABLE public.user_dazi_level IS '用户在某偶像下的靶子等级和经验';

-- =====================================================
-- 9. coin_transaction_records 金币交易记录表
-- =====================================================
CREATE TABLE IF NOT EXISTS public.coin_transaction_records (
    id BIGSERIAL PRIMARY KEY,
    user_id BIGINT NOT NULL,
    star_id BIGINT NOT NULL,
    transaction_type VARCHAR(50) NOT NULL,
    amount BIGINT NOT NULL,
    balance_before BIGINT NOT NULL,
    balance_after BIGINT NOT NULL,
    reference_id VARCHAR(100),
    reference_type VARCHAR(50),
    description VARCHAR(255),
    created_at BIGINT NOT NULL,
    CONSTRAINT uk_coin_transaction_records_id UNIQUE (id)
);

CREATE INDEX IF NOT EXISTS idx_coin_transaction_records_user ON public.coin_transaction_records USING btree (user_id);
CREATE INDEX IF NOT EXISTS idx_coin_transaction_records_star ON public.coin_transaction_records USING btree (star_id);
CREATE INDEX IF NOT EXISTS idx_coin_transaction_records_created ON public.coin_transaction_records USING btree (created_at DESC);

COMMENT ON TABLE public.coin_transaction_records IS '金币交易记录表';

-- =====================================================
-- 10. crystal_transaction_records 水晶交易记录表
-- =====================================================
CREATE TABLE IF NOT EXISTS public.crystal_transaction_records (
    id BIGSERIAL PRIMARY KEY,
    user_id BIGINT NOT NULL,
    star_id BIGINT NOT NULL,
    change_type VARCHAR(50) NOT NULL,
    delta BIGINT NOT NULL,
    balance_before BIGINT NOT NULL,
    balance_after BIGINT NOT NULL,
    source_id VARCHAR(100),
    reference_type VARCHAR(50),
    description VARCHAR(255),
    created_at BIGINT NOT NULL,
    CONSTRAINT uk_crystal_transaction_records_id UNIQUE (id)
);

CREATE INDEX IF NOT EXISTS idx_crystal_transaction_records_user ON public.crystal_transaction_records USING btree (user_id);
CREATE INDEX IF NOT EXISTS idx_crystal_transaction_records_star ON public.crystal_transaction_records USING btree (star_id);
CREATE INDEX IF NOT EXISTS idx_crystal_transaction_records_created ON public.crystal_transaction_records USING btree (created_at DESC);

COMMENT ON TABLE public.crystal_transaction_records IS '水晶交易记录表';

-- =====================================================
-- 11. mint_reward_config 铸爱奖励配置表
-- =====================================================
CREATE TABLE IF NOT EXISTS public.mint_reward_config (
    id BIGSERIAL PRIMARY KEY,
    mint_count INT NOT NULL,
    reward_type VARCHAR(50) NOT NULL,
    reward_amount BIGINT NOT NULL DEFAULT 0,
    probability BIGINT NOT NULL DEFAULT 0,
    description VARCHAR(255),
    updated_at BIGINT NOT NULL,
    CONSTRAINT uk_mint_reward_config_mint_count_type UNIQUE (mint_count, reward_type)
);

COMMENT ON TABLE public.mint_reward_config IS '铸爱奖励配置表';

-- =====================================================
-- 12. mint_milestone_config 铸爱里程碑配置表
-- =====================================================
CREATE TABLE IF NOT EXISTS public.mint_milestone_config (
    id BIGSERIAL PRIMARY KEY,
    milestone INT NOT NULL,
    reward_type VARCHAR(50) NOT NULL,
    reward_amount BIGINT NOT NULL DEFAULT 0,
    description VARCHAR(255),
    updated_at BIGINT NOT NULL,
    CONSTRAINT uk_mint_milestone_config_milestone UNIQUE (milestone)
);

COMMENT ON TABLE public.mint_milestone_config IS '铸爱里程碑配置表';

COMMIT;

-- 插入默认等级阈值数据
INSERT INTO public.level_thresholds (level, exp_requirement, title, badge_url, privileges, updated_at)
VALUES
    (1, 0, '初入星途', NULL, NULL, 1773322573872),
    (2, 100, '小小星星', NULL, NULL, 1773322573872),
    (3, 300, '新星闪耀', NULL, NULL, 1773322573872),
    (4, 600, '潜力新星', NULL, NULL, 1773322573872),
    (5, 1000, '明日之星', NULL, NULL, 1773322573872),
    (6, 1500, '冉冉升起', NULL, NULL, 1773322573872),
    (7, 2100, '星光熠熠', NULL, NULL, 1773322573872),
    (8, 2800, '璀璨星辰', NULL, NULL, 1773322573872),
    (9, 3600, '星途璀璨', NULL, NULL, 1773322573872),
    (10, 4500, '超级新星', NULL, NULL, 1773322573872)
ON CONFLICT (level) DO UPDATE SET
    exp_requirement = EXCLUDED.exp_requirement,
    title = EXCLUDED.title,
    badge_url = EXCLUDED.badge_url,
    privileges = EXCLUDED.privileges,
    updated_at = EXCLUDED.updated_at;

-- 插入默认靶子等级阈值数据
INSERT INTO public.dazi_level_thresholds (level, exp_requirement, title, updated_at)
VALUES
    (1, 0, '初级靶子', 1773322573872),
    (2, 50, '中级靶子', 1773322573872),
    (3, 150, '高级靶子', 1773322573872),
    (4, 300, '超级靶子', 1773322573872),
    (5, 500, '终极靶子', 1773322573872)
ON CONFLICT (level) DO UPDATE SET
    exp_requirement = EXCLUDED.exp_requirement,
    title = EXCLUDED.title,
    updated_at = EXCLUDED.updated_at;

-- 插入铸爱里程碑配置
INSERT INTO public.mint_milestone_config (milestone, reward_type, reward_amount, description, updated_at)
VALUES
    (10, '称号', 1, '铸爱达人', 1773322573872),
    (50, '称号', 2, '铸爱巨星', 1773322573872),
    (100, '称号', 3, '铸爱传奇', 1773322573872),
    (200, '称号', 4, '铸爱神话', 1773322573872),
    (500, '称号', 5, '铸爱永恒', 1773322573872)
ON CONFLICT (milestone) DO UPDATE SET
    reward_type = EXCLUDED.reward_type,
    reward_amount = EXCLUDED.reward_amount,
    description = EXCLUDED.description,
    updated_at = EXCLUDED.updated_at;