-- Migration: Add Groq backend support to ai_models table
-- Description: Adds backend_type, groq-specific columns, and inserts Groq production models
-- Date: 2025-01-XX

-- 1. Add backend_type and Groq-specific columns to ai_models
ALTER TABLE ai_models
ADD COLUMN IF NOT EXISTS backend_type ENUM('g4f','groq','auto') DEFAULT 'groq' COMMENT 'Which AI backend this model uses',
ADD COLUMN IF NOT EXISTS groq_model_id VARCHAR(255) DEFAULT NULL COMMENT 'Exact Groq model ID (e.g. llama-3.3-70b-versatile)',
ADD COLUMN IF NOT EXISTS context_window INT DEFAULT NULL COMMENT 'Context window size in tokens',
ADD COLUMN IF NOT EXISTS max_output_tokens INT DEFAULT NULL COMMENT 'Maximum output tokens supported',
ADD COLUMN IF NOT EXISTS supports_vision BOOLEAN DEFAULT FALSE COMMENT 'Whether model supports image inputs',
ADD COLUMN IF NOT EXISTS supports_tools BOOLEAN DEFAULT FALSE COMMENT 'Whether model supports tool/function calling',
ADD COLUMN IF NOT EXISTS supports_streaming BOOLEAN DEFAULT TRUE COMMENT 'Whether model supports streaming',
ADD COLUMN IF NOT EXISTS supports_json_mode BOOLEAN DEFAULT FALSE COMMENT 'Whether model supports JSON response format',
ADD COLUMN IF NOT EXISTS rate_limit_rpm INT DEFAULT NULL COMMENT 'Requests per minute limit',
ADD COLUMN IF NOT EXISTS rate_limit_tpm INT DEFAULT NULL COMMENT 'Tokens per minute limit',
ADD COLUMN IF NOT EXISTS tokens_per_second INT DEFAULT NULL COMMENT 'Approximate tokens per second throughput',
ADD COLUMN IF NOT EXISTS is_free BOOLEAN DEFAULT FALSE COMMENT 'Whether this model is free for all users';

-- Add index on backend_type
ALTER TABLE ai_models
ADD INDEX IF NOT EXISTS idx_backend_type (backend_type);

-- 2. Mark all existing g4f models
UPDATE ai_models 
SET backend_type = 'g4f' 
WHERE groq_model_id IS NULL;

-- 3. Insert Groq production models
INSERT INTO ai_models (
    name, description, tier, model, provider, 
    backend_type, groq_model_id,
    status, capabilities, max_tokens,
    context_window, max_output_tokens,
    supports_vision, supports_tools, supports_streaming, supports_json_mode,
    rate_limit_rpm, rate_limit_tpm, tokens_per_second,
    cost_per_1k_input_tokens, cost_per_1k_output_tokens,
    is_free, auth_required
) VALUES 
(
    'groq-llama-3.3-70b', 
    'Llama 3.3 70B Versatile via Groq — fast, reliable, best general-purpose model',
    'free', 'llama-3.3-70b-versatile', 'Groq',
    'groq', 'llama-3.3-70b-versatile',
    'active', '["text", "code", "analysis"]', 32768,
    128000, 32768,
    FALSE, TRUE, TRUE, TRUE,
    30, 20000, 280,
    0.59, 0.79,
    TRUE, FALSE
),
(
    'groq-llama-3.1-8b',
    'Llama 3.1 8B Instant via Groq — ultra-fast, lightweight for simple tasks',
    'free', 'llama-3.1-8b-instant', 'Groq',
    'groq', 'llama-3.1-8b-instant',
    'active', '["text", "code"]', 8192,
    131072, 8192,
    FALSE, TRUE, TRUE, TRUE,
    30, 20000, 560,
    0.05, 0.08,
    TRUE, FALSE
),
(
    'groq-gpt-oss-20b',
    'OpenAI GPT OSS 20B via Groq — fast open-source model',
    'free', 'openai/gpt-oss-20b', 'Groq',
    'groq', 'openai/gpt-oss-20b',
    'active', '["text", "code"]', 16384,
    128000, 16384,
    FALSE, TRUE, TRUE, TRUE,
    30, 10000, 1000,
    0.10, 0.30,
    TRUE, FALSE
),
(
    'groq-gpt-oss-120b',
    'OpenAI GPT OSS 120B via Groq — powerful open-source model',
    'free', 'openai/gpt-oss-120b', 'Groq',
    'groq', 'openai/gpt-oss-120b',
    'active', '["text", "code", "analysis"]', 16384,
    128000, 16384,
    FALSE, TRUE, TRUE, TRUE,
    15, 6000, 500,
    0.30, 1.20,
    TRUE, FALSE
),
(
    'groq-llama-4-scout',
    'Llama 4 Scout 17B via Groq — vision-capable model for image analysis',
    'free', 'meta-llama/llama-4-scout-17b-16e-instruct', 'Groq',
    'groq', 'meta-llama/llama-4-scout-17b-16e-instruct',
    'active', '["text", "code", "vision"]', 8192,
    131072, 8192,
    TRUE, TRUE, TRUE, TRUE,
    30, 15000, 750,
    0.15, 0.40,
    TRUE, FALSE
),
(
    'groq-qwen3-32b',
    'Qwen 3 32B via Groq — strong multilingual model with thinking mode',
    'free', 'qwen/qwen3-32b', 'Groq',
    'groq', 'qwen/qwen3-32b',
    'active', '["text", "code", "analysis"]', 16384,
    131072, 16384,
    FALSE, TRUE, TRUE, TRUE,
    30, 14400, 400,
    0.34, 0.34,
    TRUE, FALSE
),
(
    'groq-kimi-k2',
    'Moonshotai Kimi K2 via Groq — large agentic model',
    'free', 'moonshotai/kimi-k2-instruct-0905', 'Groq',
    'groq', 'moonshotai/kimi-k2-instruct-0905',
    'active', '["text", "code", "analysis"]', 16384,
    131072, 16384,
    FALSE, TRUE, TRUE, TRUE,
    30, 10000, 200,
    0.20, 0.60,
    TRUE, FALSE
),
(
    'groq-compound',
    'Groq Compound Beta — multi-step agentic reasoning with web search',
    'free', 'compound-beta', 'Groq',
    'groq', 'compound-beta',
    'active', '["text", "analysis", "search"]', 8192,
    128000, 8192,
    FALSE, TRUE, TRUE, TRUE,
    15, 10000, 0,
    0.00, 0.00,
    TRUE, FALSE
)
ON DUPLICATE KEY UPDATE 
    groq_model_id = VALUES(groq_model_id),
    backend_type = VALUES(backend_type),
    context_window = VALUES(context_window),
    max_output_tokens = VALUES(max_output_tokens),
    supports_vision = VALUES(supports_vision),
    supports_tools = VALUES(supports_tools),
    supports_streaming = VALUES(supports_streaming),
    supports_json_mode = VALUES(supports_json_mode),
    rate_limit_rpm = VALUES(rate_limit_rpm),
    rate_limit_tpm = VALUES(rate_limit_tpm),
    tokens_per_second = VALUES(tokens_per_second),
    updated_at = CURRENT_TIMESTAMP;

-- 4. Update users default model preference to Groq
UPDATE users 
SET preferred_ai_model = 'groq-llama-3.3-70b' 
WHERE preferred_ai_model = 'llama-3.3-70b' OR preferred_ai_model IS NULL;

-- 5. Verify migration
SELECT name, backend_type, groq_model_id, supports_vision, tokens_per_second 
FROM ai_models 
ORDER BY backend_type, name;
