-- 동물약 챗봇 로그 스키마
-- 생성일: 2026-03-08

CREATE TABLE IF NOT EXISTS chat_logs (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    session_id TEXT,
    
    -- 입력
    user_message TEXT,
    history_length INTEGER,
    
    -- MSSQL (보유 동물약)
    mssql_drug_count INTEGER,
    mssql_duration_ms INTEGER,
    
    -- PostgreSQL (RAG)
    pgsql_rag_count INTEGER,
    pgsql_duration_ms INTEGER,
    
    -- LanceDB (벡터 검색)
    vector_results_count INTEGER,
    vector_top_scores TEXT,  -- JSON: [0.92, 0.85, 0.78]
    vector_sources TEXT,     -- JSON: ["file1.md#section", ...]
    vector_duration_ms INTEGER,
    
    -- OpenAI
    openai_model TEXT,
    openai_prompt_tokens INTEGER,
    openai_completion_tokens INTEGER,
    openai_total_tokens INTEGER,
    openai_cost_usd REAL,
    openai_duration_ms INTEGER,
    
    -- 출력
    assistant_response TEXT,
    products_mentioned TEXT,  -- JSON array
    
    -- 메타
    total_duration_ms INTEGER,
    error TEXT,
    created_at TEXT DEFAULT (datetime('now', 'localtime'))
);

-- 인덱스
CREATE INDEX IF NOT EXISTS idx_chat_created ON chat_logs(created_at);
CREATE INDEX IF NOT EXISTS idx_chat_session ON chat_logs(session_id);
CREATE INDEX IF NOT EXISTS idx_chat_error ON chat_logs(error);
