-- 铸造经济：按「第几次铸爱」阶梯扣水晶（见 docs/specs/2026-04-15-economic-system-design.md）
-- 未建表/未插数时，资产服务查配置会得到 record not found，网关表现为 code 17。

-- 用户在某 star 下的累计铸爱次数（CreateMintOrder 会读/写）
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 '用户在某偶像下的累计铸爱次数与收益加成（基点）';

-- 第 N 次铸爱对应扣费与保底概率（mint_count 唯一）
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;
