-- Migration 015: Tool Documentation
-- Creates tool_docs table for queryable tool documentation with semantic search
-- Dependencies: 001_base_schema.sql (pgvector extension)

-- Tool documentation table
CREATE TABLE IF NOT EXISTS tool_docs (
    id SERIAL PRIMARY KEY,
    tool_name TEXT NOT NULL,
    category TEXT NOT NULL CHECK (category IN ('mcp', 'cli', 'script', 'internal', 'deprecated')),
    title TEXT NOT NULL,
    description TEXT NOT NULL,
    usage_example TEXT,
    parameters JSONB,  -- Structured parameter definitions
    notes TEXT,  -- Additional notes, gotchas, tips
    tags TEXT[],  -- Searchable tags (e.g., ['backup', 'database', 'postgresql'])
    source_file TEXT,  -- Original source file (TOOLS.md, script path, etc.)
    embedding vector(1024),  -- Semantic search embedding
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Indexes for fast lookups
CREATE INDEX IF NOT EXISTS idx_tool_docs_name ON tool_docs(tool_name);
CREATE INDEX IF NOT EXISTS idx_tool_docs_category ON tool_docs(category);
CREATE INDEX IF NOT EXISTS idx_tool_docs_tags ON tool_docs USING gin(tags);

-- HNSW index for semantic similarity search
CREATE INDEX IF NOT EXISTS idx_tool_docs_embedding ON tool_docs USING hnsw (embedding vector_cosine_ops);

-- Full-text search on title + description
CREATE INDEX IF NOT EXISTS idx_tool_docs_fts ON tool_docs
    USING gin(to_tsvector('english', title || ' ' || description || ' ' || COALESCE(notes, '')));

-- Record migration
INSERT INTO schema_migrations (version) VALUES ('015_tool_docs')
ON CONFLICT (version) DO NOTHING;
