-- AI Models Configuration Table
-- This table stores AI model configurations dynamically
-- Instead of hardcoding models, they are fetched from this database

CREATE TABLE IF NOT EXISTS `ai_models` (
  `id` INT PRIMARY KEY AUTO_INCREMENT,
  `name` VARCHAR(255) UNIQUE NOT NULL COMMENT 'Unique identifier for the model',
  `description` TEXT COMMENT 'User-friendly description of the model',
  `tier` ENUM('free', 'paid', 'premium') DEFAULT 'free' COMMENT 'Pricing tier',
  `model` VARCHAR(255) NOT NULL COMMENT 'Actual model name to pass to provider',
  `provider` VARCHAR(100) NOT NULL COMMENT 'AI provider (e.g., PollinationsAI, OpenAI, etc.)',
  `api_key` TEXT COMMENT 'API key if required (encrypted)',
  `base_url` VARCHAR(500) COMMENT 'Custom API base URL if needed',
  `status` ENUM('active', 'inactive', 'working', 'deprecated') DEFAULT 'active' COMMENT 'Model status',
  `capabilities` JSON COMMENT 'Array of capabilities: ["text", "code", "image", "vision"]',
  `max_tokens` INT DEFAULT 8192 COMMENT 'Maximum token limit',
  `auth_required` BOOLEAN DEFAULT FALSE COMMENT 'Whether authentication is required',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  KEY `idx_provider` (`provider`),
  KEY `idx_status` (`status`),
  KEY `idx_tier` (`tier`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci 
COMMENT='Dynamic AI model configurations for the platform';

-- Insert default models (geminisearch via PollinationsAI as the primary free model)
INSERT INTO `ai_models` (`name`, `description`, `tier`, `model`, `provider`, `status`, `capabilities`, `max_tokens`, `auth_required`) 
VALUES 
  ('geminisearch', 'Gemini Search via Pollinations AI', 'free', 'geminisearch', 'PollinationsAI', 'working', '["text", "code", "vision"]', 32768, FALSE),
  ('gpt-4o', 'GPT-4 Optimized via Pollinations AI', 'free', 'gpt-4o', 'PollinationsAI', 'working', '["text", "code", "vision"]', 128000, FALSE),
  ('claude-3.5-sonnet', 'Claude 3.5 Sonnet via Pollinations AI', 'free', 'claude-3.5-sonnet', 'PollinationsAI', 'working', '["text", "code"]', 200000, FALSE),
  ('llama-3.3-70b', 'Llama 3.3 70B via Pollinations AI', 'free', 'llama-3.3-70b', 'PollinationsAI', 'working', '["text", "code"]', 32768, FALSE),
  ('mistral-large', 'Mistral Large via Pollinations AI', 'free', 'mistral-large', 'PollinationsAI', 'working', '["text", "code"]', 32768, FALSE),
  ('deepseek-chat', 'DeepSeek Chat via Pollinations AI', 'free', 'deepseek-chat', 'PollinationsAI', 'working', '["text", "code"]', 64000, FALSE),
  ('qwen-2.5-coder-32b', 'Qwen 2.5 Coder 32B via Pollinations AI', 'free', 'qwen-2.5-coder-32b', 'PollinationsAI', 'working', '["code", "text"]', 32768, FALSE)
ON DUPLICATE KEY UPDATE updated_at = CURRENT_TIMESTAMP;

-- Note: MariaDB doesn't support functional indexes on JSON columns the same way as PostgreSQL
-- JSON searching is done using JSON_CONTAINS() function which is efficient enough without index

