-- ============================================
-- 运营活动相关表
-- ============================================

-- 11. activities 表 - 运营活动表
CREATE TABLE IF NOT EXISTS activities (
    id BIGSERIAL PRIMARY KEY,
    activity_type VARCHAR(50) NOT NULL,       -- 活动类型: birthday/concert/bus
    title VARCHAR(100) NOT NULL,              -- 活动标题
    description TEXT,                         -- 活动描述
    star_id BIGINT NOT NULL,                  -- 所属明星
    start_time BIGINT NOT NULL,               -- 开始时间 (毫秒时间戳)
    end_time BIGINT NOT NULL,                 -- 结束时间 (毫秒时间戳)
    target_progress BIGINT NOT NULL DEFAULT 1000,  -- 目标进度
    current_progress BIGINT NOT NULL DEFAULT 0,     -- 当前进度
    status VARCHAR(20) NOT NULL DEFAULT 'pending',  -- pending/active/completed/expired
    stage_configs JSONB,                       -- 阶段配置 (不同进度的背景图等)
    created_at BIGINT NOT NULL,
    updated_at BIGINT NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_activities_star_id ON activities(star_id);
CREATE INDEX IF NOT EXISTS idx_activities_status ON activities(status);
CREATE INDEX IF NOT EXISTS idx_activities_start_end ON activities(start_time, end_time);

-- 12. activity_items 表 - 活动道具表 (数据库配置，支持运营平台动态调整)
CREATE TABLE IF NOT EXISTS activity_items (
    id BIGSERIAL PRIMARY KEY,
    activity_id BIGINT NOT NULL,
    item_type VARCHAR(50) NOT NULL,           -- 道具类型: firework/megaphone/love
    item_name VARCHAR(50) NOT NULL,           -- 道具名称
    icon_url VARCHAR(500),                     -- 道具图标
    crystal_cost INTEGER NOT NULL,            -- 水晶消耗
    contribution_points INTEGER NOT NULL,     -- 贡献点奖励
    sort_order INTEGER NOT NULL DEFAULT 0,
    is_active BOOLEAN NOT NULL DEFAULT true,
    created_at BIGINT NOT NULL,
    updated_at BIGINT NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_activity_items_activity ON activity_items(activity_id);
CREATE INDEX IF NOT EXISTS idx_activity_items_type ON activity_items(item_type);

-- 13. activity_contributions 表 - 用户活动贡献记录表
CREATE TABLE IF NOT EXISTS activity_contributions (
    id BIGSERIAL PRIMARY KEY,
    activity_id BIGINT NOT NULL,
    user_id BIGINT NOT NULL,
    star_id BIGINT NOT NULL,
    item_id BIGINT NOT NULL,
    item_type VARCHAR(50) NOT NULL,
    quantity INTEGER NOT NULL DEFAULT 1,
    crystal_spent BIGINT NOT NULL,
    contribution_points BIGINT NOT NULL,
    created_at BIGINT NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_activity_contributions_activity ON activity_contributions(activity_id);
CREATE INDEX IF NOT EXISTS idx_activity_contributions_user_star ON activity_contributions(user_id, star_id);
CREATE INDEX IF NOT EXISTS idx_activity_contributions_created ON activity_contributions(created_at DESC);

-- 14. activity_user_stats 表 - 用户活动贡献汇总表 (用于排行榜)
CREATE TABLE IF NOT EXISTS activity_user_stats (
    id BIGSERIAL PRIMARY KEY,
    activity_id BIGINT NOT NULL,
    user_id BIGINT NOT NULL,
    star_id BIGINT NOT NULL,
    total_contribution BIGINT NOT NULL DEFAULT 0,
    total_crystal_spent BIGINT NOT NULL DEFAULT 0,
    total_items INTEGER NOT NULL DEFAULT 0,
    last_contribute_at BIGINT NOT NULL,
    created_at BIGINT NOT NULL,
    updated_at BIGINT NOT NULL,

    CONSTRAINT uk_activity_user_star UNIQUE (activity_id, user_id, star_id)
);

CREATE INDEX IF NOT EXISTS idx_activity_user_stats_activity ON activity_user_stats(activity_id);
CREATE INDEX IF NOT EXISTS idx_activity_user_stats_contribution ON activity_user_stats(activity_id, total_contribution DESC);
CREATE INDEX IF NOT EXISTS idx_activity_user_stats_user_star ON activity_user_stats(user_id, star_id);

-- 外键约束
ALTER TABLE activity_items
    ADD CONSTRAINT fk_activity_items_activity FOREIGN KEY (activity_id) REFERENCES activities(id) ON DELETE CASCADE;

ALTER TABLE activity_contributions
    ADD CONSTRAINT fk_activity_contributions_activity FOREIGN KEY (activity_id) REFERENCES activities(id) ON DELETE CASCADE,
    ADD CONSTRAINT fk_activity_contributions_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    ADD CONSTRAINT fk_activity_contributions_item FOREIGN KEY (item_id) REFERENCES activity_items(id) ON DELETE CASCADE,
    ADD CONSTRAINT fk_activity_contributions_star FOREIGN KEY (star_id) REFERENCES stars(star_id) ON DELETE CASCADE;

ALTER TABLE activity_user_stats
    ADD CONSTRAINT fk_activity_user_stats_activity FOREIGN KEY (activity_id) REFERENCES activities(id) ON DELETE CASCADE,
    ADD CONSTRAINT fk_activity_user_stats_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    ADD CONSTRAINT fk_activity_user_stats_star FOREIGN KEY (star_id) REFERENCES stars(star_id) ON DELETE CASCADE;

-- ============================================
-- 注释说明
-- ============================================
COMMENT ON TABLE activities IS '运营活动表';
COMMENT ON TABLE activity_items IS '活动道具表';
COMMENT ON TABLE activity_contributions IS '用户活动贡献记录表';
COMMENT ON TABLE activity_user_stats IS '用户活动贡献汇总表';
