-- Enable pgvector extension for RAG functionality
CREATE EXTENSION IF NOT EXISTS vector;

-- Create knowledge_base table for RAG
CREATE TABLE IF NOT EXISTS knowledge_base (
    id VARCHAR(255) PRIMARY KEY,
    tenant_id VARCHAR(255) NOT NULL,
    title VARCHAR(500),
    content TEXT NOT NULL,
    embedding vector(1536), -- OpenAI ada-002 dimension, adjust as needed
    metadata JSONB,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
    INDEX idx_tenant (tenant_id)
);

-- Create index for vector similarity search
CREATE INDEX IF NOT EXISTS idx_knowledge_embedding ON knowledge_base 
USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 100);

