-- users 表：存储业务用户
CREATE TABLE IF NOT EXISTS users (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    email VARCHAR(255) UNIQUE NOT NULL,
    password_hash VARCHAR(255),
    subscription_tier VARCHAR(50) DEFAULT 'Free', -- Free, Pro, Premium
    identity_label VARCHAR(100), -- AI 创始人, SaaS Builder 等
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

-- monitored_accounts 表：存储用户重点监控的 X 账号
CREATE TABLE IF NOT EXISTS monitored_accounts (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    x_account_id VARCHAR(255),
    x_handle VARCHAR(255) NOT NULL,
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    UNIQUE (user_id, x_handle)
);

-- monitored_keywords 表：存储用户重点监控的关键词
CREATE TABLE IF NOT EXISTS monitored_keywords (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    keyword VARCHAR(255) NOT NULL,
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    UNIQUE (user_id, keyword)
);

-- tweets 表：共享的推文数据池，AI 评论生成的上下文
CREATE TABLE IF NOT EXISTS tweets (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    x_tweet_id VARCHAR(255) UNIQUE NOT NULL,
    author_id VARCHAR(255),
    author_handle VARCHAR(255),
    content TEXT NOT NULL,
    posted_at TIMESTAMP WITH TIME ZONE,
    like_count INTEGER DEFAULT 0,
    retweet_count INTEGER DEFAULT 0,
    reply_count INTEGER DEFAULT 0,
    heat_score FLOAT DEFAULT 0.0,
    is_processed BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_tweets_x_tweet_id ON tweets(x_tweet_id);
CREATE INDEX idx_tweets_heat_score ON tweets(heat_score DESC);

-- generated_replies 表：生成的 AI 评论记录
CREATE TABLE IF NOT EXISTS generated_replies (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    tweet_id UUID NOT NULL REFERENCES tweets(id) ON DELETE CASCADE,
    strategy_type VARCHAR(100) NOT NULL, -- 认知升级型, 反向观点型, 数据补充型, 共鸣型, 创始人经验型
    content TEXT NOT NULL,
    status VARCHAR(50) DEFAULT 'draft', -- draft, copied, posted
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_generated_replies_user_id ON generated_replies(user_id);
CREATE INDEX idx_generated_replies_tweet_id ON generated_replies(tweet_id);

-- reply_performance 表：针对已发布评论的效果数据回拨
CREATE TABLE IF NOT EXISTS reply_performance (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    reply_id UUID NOT NULL REFERENCES generated_replies(id) ON DELETE CASCADE,
    like_count_increase INTEGER DEFAULT 0,
    reply_count_increase INTEGER DEFAULT 0,
    interaction_rate FLOAT DEFAULT 0.0,
    check_time TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_reply_performance_reply_id ON reply_performance(reply_id);

-- 更新 updated_at 的触发器函数
CREATE OR REPLACE FUNCTION update_modified_column()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ language 'plpgsql';

-- 为 users 表添加触发器
CREATE TRIGGER update_users_modtime
    BEFORE UPDATE ON users
    FOR EACH ROW
    EXECUTE FUNCTION update_modified_column();
