-- 004_user_scenarios.up.sql
-- 用户自建情景表

CREATE TABLE user_scenarios (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    name VARCHAR(50) NOT NULL,
    icon VARCHAR(10) DEFAULT '✨',
    description VARCHAR(100) NOT NULL,
    prompt TEXT NOT NULL,
    greeting VARCHAR(200),
    language VARCHAR(10) DEFAULT 'zh-CN',
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP NOT NULL DEFAULT NOW(),

    CONSTRAINT unique_user_scenario UNIQUE(user_id, name),
    CONSTRAINT check_name_length CHECK (char_length(name) >= 2 AND char_length(name) <= 50),
    CONSTRAINT check_description_length CHECK (char_length(description) >= 5 AND char_length(description) <= 100),
    CONSTRAINT check_prompt_length CHECK (char_length(prompt) >= 50 AND char_length(prompt) <= 2000)
);

-- 为用户 ID 创建索引，加速查询
CREATE INDEX idx_user_scenarios_user_id ON user_scenarios(user_id);

-- 为创建时间创建索引，用于排序
CREATE INDEX idx_user_scenarios_created_at ON user_scenarios(created_at DESC);

COMMENT ON TABLE user_scenarios IS '用户自建情景表';
COMMENT ON COLUMN user_scenarios.id IS '情景唯一标识';
COMMENT ON COLUMN user_scenarios.user_id IS '所属用户 ID，外键关联 users 表';
COMMENT ON COLUMN user_scenarios.name IS '情景名称，如"创意写作导师"';
COMMENT ON COLUMN user_scenarios.icon IS 'Emoji 图标，如"🎨"';
COMMENT ON COLUMN user_scenarios.description IS '简短描述，显示在情景卡片上';
COMMENT ON COLUMN user_scenarios.prompt IS '角色 System Prompt，定义 AI 行为';
COMMENT ON COLUMN user_scenarios.greeting IS '首句引导，可选';
COMMENT ON COLUMN user_scenarios.language IS '默认语言，如 zh-CN、en-US';
