-- =====================================================
-- 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);

-- 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);
