-- MySQL initialization script for Dialectic

CREATE DATABASE IF NOT EXISTS dialectic;
USE dialectic;

-- Table for storing API keys for different providers
CREATE TABLE IF NOT EXISTS api_keys (
    id INT AUTO_INCREMENT PRIMARY KEY,
    provider VARCHAR(50) NOT NULL UNIQUE,
    api_key_encrypted TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Table for storing model configurations
CREATE TABLE IF NOT EXISTS model_configs (
    id INT AUTO_INCREMENT PRIMARY KEY,
    provider VARCHAR(50) NOT NULL,
    model_name VARCHAR(100) NOT NULL,
    display_name VARCHAR(100),
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY unique_provider_model (provider, model_name)
);

-- Insert default API key records (encrypted field will be empty initially)
INSERT IGNORE INTO api_keys (provider, api_key_encrypted) VALUES
('openai', ''),
('claude', ''),
('qwen', ''),
('deepseek', '');

-- Insert default model configurations
INSERT IGNORE INTO model_configs (provider, model_name, display_name) VALUES
('openai', 'gpt-4', 'GPT-4'),
('openai', 'gpt-3.5-turbo', 'GPT-3.5 Turbo'),
('claude', 'claude-3-opus', 'Claude 3 Opus'),
('claude', 'claude-3-sonnet', 'Claude 3 Sonnet'),
('claude', 'claude-3-haiku', 'Claude 3 Haiku'),
('qwen', 'qwen-max', 'Qwen Max'),
('qwen', 'qwen-plus', 'Qwen Plus'),
('deepseek', 'deepseek-chat', 'DeepSeek Chat'),
('deepseek', 'deepseek-coder', 'DeepSeek Coder');

-- Add evidence_library column to debate_sessions (migration for existing databases)
ALTER TABLE debate_sessions ADD COLUMN IF NOT EXISTS evidence_library TEXT NULL;