-- Migration: 001_initial_schema
-- Author: Descomplicar®
-- Date: 2026-02-01
-- Description: Tabelas core da infraestrutura Claude Code

-- UP

-- Agentes especializados
CREATE TABLE IF NOT EXISTS cr_agents (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE,
    description TEXT,
    model VARCHAR(50) DEFAULT 'sonnet',
    category VARCHAR(50),
    status ENUM('active', 'inactive', 'deprecated') DEFAULT 'active',
    desk_task INT,
    file_path VARCHAR(255),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Skills invocáveis
CREATE TABLE IF NOT EXISTS cr_skills (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE,
    description TEXT,
    category VARCHAR(50),
    status ENUM('active', 'inactive', 'deprecated') DEFAULT 'active',
    desk_task INT,
    file_path VARCHAR(255),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Servidores MCP
CREATE TABLE IF NOT EXISTS cr_mcps (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE,
    description TEXT,
    transport ENUM('stdio', 'sse', 'http') DEFAULT 'stdio',
    status ENUM('active', 'inactive', 'error') DEFAULT 'active',
    gateway_url VARCHAR(255),
    tools_count INT DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Software Development Kits
CREATE TABLE IF NOT EXISTS cr_sdks (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE,
    description TEXT,
    category VARCHAR(50),
    status ENUM('active', 'inactive', 'deprecated') DEFAULT 'active',
    version VARCHAR(20),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Ferramentas por MCP
CREATE TABLE IF NOT EXISTS cr_mcp_tools (
    id INT AUTO_INCREMENT PRIMARY KEY,
    mcp_id INT NOT NULL,
    name VARCHAR(100) NOT NULL,
    description TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (mcp_id) REFERENCES cr_mcps(id) ON DELETE CASCADE,
    UNIQUE KEY unique_mcp_tool (mcp_id, name)
);

-- Plugins instalados
CREATE TABLE IF NOT EXISTS cr_plugins (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE,
    version VARCHAR(20),
    marketplace VARCHAR(100),
    status ENUM('active', 'disabled', 'pending') DEFAULT 'active',
    installed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Hooks configurados
CREATE TABLE IF NOT EXISTS cr_hooks (
    id INT AUTO_INCREMENT PRIMARY KEY,
    event VARCHAR(50) NOT NULL,
    plugin_name VARCHAR(100),
    command TEXT NOT NULL,
    description TEXT,
    timeout INT DEFAULT 5000,
    status ENUM('active', 'disabled') DEFAULT 'active',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Índices
CREATE INDEX idx_agents_status ON cr_agents(status);
CREATE INDEX idx_agents_category ON cr_agents(category);
CREATE INDEX idx_skills_status ON cr_skills(status);
CREATE INDEX idx_skills_category ON cr_skills(category);
CREATE INDEX idx_mcps_status ON cr_mcps(status);
CREATE INDEX idx_sdks_status ON cr_sdks(status);

-- DOWN
DROP TABLE IF EXISTS cr_hooks;
DROP TABLE IF EXISTS cr_plugins;
DROP TABLE IF EXISTS cr_mcp_tools;
DROP TABLE IF EXISTS cr_sdks;
DROP TABLE IF EXISTS cr_mcps;
DROP TABLE IF EXISTS cr_skills;
DROP TABLE IF EXISTS cr_agents;
