-- Migration: Add pgvector support to icd_codes table

-- Enable pgvector extension
CREATE EXTENSION IF NOT EXISTS vector;

-- Add embedding column with pgvector type
ALTER TABLE "icd_codes" ADD COLUMN IF NOT EXISTS "embedding" vector(1536);

-- Add metadata column for LangChain pgvector
ALTER TABLE "icd_codes" ADD COLUMN IF NOT EXISTS "metadata" JSONB;

-- Add content column for LangChain pgvector
ALTER TABLE "icd_codes" ADD COLUMN IF NOT EXISTS "content" TEXT;

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

-- Create index on metadata for fast JSON queries
CREATE INDEX IF NOT EXISTS "icd_codes_metadata_idx" ON "icd_codes" USING GIN (metadata);

-- Add comments for documentation
COMMENT ON COLUMN "icd_codes"."embedding" IS 'Vector embedding for semantic search using pgvector (1536 dimensions)';
COMMENT ON COLUMN "icd_codes"."metadata" IS 'JSON metadata for LangChain pgvector operations';
COMMENT ON COLUMN "icd_codes"."content" IS 'Text content for LangChain pgvector operations';
COMMENT ON INDEX "icd_codes_embedding_idx" IS 'IVFFlat index for fast cosine similarity search with 100 lists';
COMMENT ON INDEX "icd_codes_metadata_idx" IS 'GIN index for fast JSON metadata queries';
