-- 开发环境默认 SQLite（config/development.py → sqlite:///dev.db）增量修复
-- 在仓库根目录执行: sqlite3 dev.db < scripts/ensure_evaluation_schema_sqlite.sql
-- 若列已存在会报错，可忽略该句或先 .schema prompt_history 查看

ALTER TABLE prompt_history ADD COLUMN comparison_group_id VARCHAR(64);
ALTER TABLE prompt_history ADD COLUMN is_comparison_enabled INTEGER NOT NULL DEFAULT 0;

-- 评价表（与 ORM 一致；若已存在可跳过整段）
CREATE TABLE IF NOT EXISTS prompt_evaluation (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    history_id INTEGER NOT NULL,
    user_id INTEGER NOT NULL,
    clarity_rating INTEGER,
    specificity_rating INTEGER,
    effectiveness_rating INTEGER,
    professionalism_rating INTEGER,
    completeness_rating INTEGER,
    is_best_version INTEGER NOT NULL DEFAULT 0,
    comparison_group_id VARCHAR(64),
    overall_rating INTEGER,
    comments TEXT,
    created_at TIMESTAMP,
    updated_at TIMESTAMP,
    FOREIGN KEY (history_id) REFERENCES prompt_history(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS comparison_group (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    group_id VARCHAR(64) NOT NULL UNIQUE,
    user_id INTEGER NOT NULL,
    name VARCHAR(100),
    description TEXT,
    is_public INTEGER NOT NULL DEFAULT 0,
    created_at TIMESTAMP,
    updated_at TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_pe_history ON prompt_evaluation(history_id);
CREATE INDEX IF NOT EXISTS idx_pe_user ON prompt_evaluation(user_id);
