-- Migration: Add kb_type, priority, is_enabled, doc_count to knowledge_bases
-- Date: 2026-02-27
-- Issue: [AC-AISVC-59] Multi-KB management fields missing

-- Add kb_type column
ALTER TABLE knowledge_bases 
ADD COLUMN IF NOT EXISTS kb_type VARCHAR DEFAULT 'general';

-- Add priority column
ALTER TABLE knowledge_bases 
ADD COLUMN IF NOT EXISTS priority INTEGER DEFAULT 0;

-- Add is_enabled column
ALTER TABLE knowledge_bases 
ADD COLUMN IF NOT EXISTS is_enabled BOOLEAN DEFAULT TRUE;

-- Add doc_count column
ALTER TABLE knowledge_bases 
ADD COLUMN IF NOT EXISTS doc_count INTEGER DEFAULT 0;

-- Add index for tenant + kb_type
CREATE INDEX IF NOT EXISTS ix_knowledge_bases_tenant_kb_type 
ON knowledge_bases (tenant_id, kb_type);

-- Update existing records to have default values
UPDATE knowledge_bases 
SET kb_type = 'general', 
    priority = 0, 
    is_enabled = TRUE, 
    doc_count = 0 
WHERE kb_type IS NULL OR priority IS NULL OR is_enabled IS NULL OR doc_count IS NULL;
