-- LLM Team UI Database Schema
-- Run against PostgreSQL: psql -d knowledge_base -f schema.sql

CREATE TABLE IF NOT EXISTS team_runs (
    id SERIAL PRIMARY KEY,
    mode TEXT NOT NULL,
    prompt TEXT NOT NULL,
    config JSONB,
    responses JSONB NOT NULL DEFAULT '[]',
    models_used TEXT[],
    created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS pipeline_runs (
    id SERIAL PRIMARY KEY,
    pipeline TEXT NOT NULL,
    topic TEXT NOT NULL,
    status TEXT DEFAULT 'running',
    steps JSONB DEFAULT '[]',
    result JSONB,
    models_used TEXT[],
    duration_ms INTEGER,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    completed_at TIMESTAMPTZ
);

CREATE TABLE IF NOT EXISTS lab_experiments (
    id SERIAL PRIMARY KEY,
    name TEXT NOT NULL,
    status TEXT DEFAULT 'idle',
    objective TEXT,
    metric TEXT DEFAULT 'quality',
    eval_cases JSONB DEFAULT '[]',
    mutable_config JSONB DEFAULT '{}',
    best_config JSONB,
    best_score FLOAT DEFAULT 0,
    total_trials INTEGER DEFAULT 0,
    improvements INTEGER DEFAULT 0,
    models_pool TEXT[],
    created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS lab_trials (
    id SERIAL PRIMARY KEY,
    experiment_id INTEGER REFERENCES lab_experiments(id) ON DELETE CASCADE,
    trial_num INTEGER,
    config_diff TEXT,
    config_snapshot JSONB,
    scores JSONB,
    avg_score FLOAT,
    improved BOOLEAN DEFAULT FALSE,
    duration_ms INTEGER,
    created_at TIMESTAMPTZ DEFAULT NOW()
);
