-- =====================================================
-- Agent System Database Migration
-- Run this script if you want vector-based memory storage
-- Note: Requires pgvector extension for PostgreSQL
-- =====================================================

-- Enable pgvector extension (PostgreSQL only)
-- CREATE EXTENSION IF NOT EXISTS vector;

-- Agent Memory with Vector Embedding (Optional - for vector search)
-- This table is optional. If not using vector search, use agent_memories table instead.
-- CREATE TABLE IF NOT EXISTS agent_memory_vectors (
--     id VARCHAR(191) PRIMARY KEY,
--     agent_id VARCHAR(191) NOT NULL,
--     user_id VARCHAR(191),
--     content TEXT NOT NULL,
--     embedding vector(1536),  -- Adjust dimension based on your embedding model
--     memory_type VARCHAR(20), -- experience/preference/conversation
--     importance INT DEFAULT 5,
--     created_at DATETIME(3),
--     updated_at DATETIME(3),
--     INDEX idx_agent_vector USING ivfflat (agent_id, embedding vector_cosine_ops),
--     INDEX idx_importance (agent_id, importance DESC)
-- );

-- For MySQL (without vector support, use text search instead)
CREATE TABLE IF NOT EXISTS agent_memory_vectors (
    id VARCHAR(191) PRIMARY KEY,
    agent_id VARCHAR(191) NOT NULL,
    user_id VARCHAR(191),
    content TEXT NOT NULL,
    embedding_text TEXT,  -- Store serialized vector for MySQL
    memory_type VARCHAR(20),
    importance INT DEFAULT 5,
    created_at DATETIME(3),
    updated_at DATETIME(3),
    INDEX idx_agent_memory_agent (agent_id),
    INDEX idx_agent_memory_user (agent_id, user_id),
    INDEX idx_importance (agent_id, importance DESC)
);

-- Agent Task Indexes
CREATE INDEX IF NOT EXISTS idx_agent_tasks_agent_user ON agent_tasks(agent_id, user_id);
CREATE INDEX IF NOT EXISTS idx_agent_tasks_session ON agent_tasks(session_id);
CREATE INDEX IF NOT EXISTS idx_agent_tasks_status ON agent_tasks(status);
CREATE INDEX IF NOT EXISTS idx_agent_tasks_created ON agent_tasks(created_at DESC);

-- Agent Team Indexes
CREATE INDEX IF NOT EXISTS idx_agent_team_supervisor ON agent_teams(supervisor_agent_id);
CREATE INDEX IF NOT EXISTS idx_agent_team_member ON agent_teams(member_agent_id);

-- Chat Sessions Table
CREATE TABLE IF NOT EXISTS chat_sessions (
    id VARCHAR(36) PRIMARY KEY,
    user_id VARCHAR(36) NOT NULL,
    agent_id VARCHAR(36),
    title VARCHAR(255),
    model_id VARCHAR(36),
    status VARCHAR(20) DEFAULT 'active',
    created_at DATETIME(3),
    updated_at DATETIME(3),
    INDEX idx_chat_sessions_user (user_id),
    INDEX idx_chat_sessions_agent (agent_id),
    INDEX idx_chat_sessions_updated (updated_at DESC)
);

-- Chat Messages Table
CREATE TABLE IF NOT EXISTS chat_messages (
    id VARCHAR(36) PRIMARY KEY,
    session_id VARCHAR(36) NOT NULL,
    role VARCHAR(20),
    content TEXT,
    tokens_used INT DEFAULT 0,
    duration_ms INT DEFAULT 0,
    metadata TEXT,
    created_at DATETIME(3),
    INDEX idx_chat_messages_session (session_id),
    INDEX idx_chat_messages_created (created_at ASC)
);

-- Chat Groups Table
CREATE TABLE IF NOT EXISTS chat_groups (
    id VARCHAR(36) PRIMARY KEY,
    user_id VARCHAR(36) NOT NULL,
    name VARCHAR(100) NOT NULL,
    description TEXT,
    agent_ids TEXT,
    status VARCHAR(20) DEFAULT 'active',
    created_at DATETIME(3),
    updated_at DATETIME(3),
    INDEX idx_chat_groups_user (user_id)
);

-- Agent Memory Indexes
CREATE INDEX IF NOT EXISTS idx_agent_memory_agent ON agent_memories(agent_id);
CREATE INDEX IF NOT EXISTS idx_agent_memory_user ON agent_memories(agent_id, user_id);
CREATE INDEX IF NOT EXISTS idx_agent_memory_type ON agent_memories(memory_type);

-- Agent Skills Indexes
CREATE INDEX IF NOT EXISTS idx_agent_skills_agent ON agent_skills(agent_id);
CREATE INDEX IF NOT EXISTS idx_agent_skills_skill ON agent_skills(skill_id);

-- Agent Knowledge Base Indexes
CREATE INDEX IF NOT EXISTS idx_agent_kb_agent ON agent_knowledge_bases(agent_id);
CREATE INDEX IF NOT EXISTS idx_agent_kb_kb ON agent_knowledge_bases(knowledge_base_id);
