-- AI Service Database Initialization Script
-- Version: 0.2.0
-- Description: Creates all required tables for AI Service with multi-tenant support
-- 
-- Usage:
--   psql -U postgres -f scripts/init_db.sql
-- Or connect to ai_service database and run this script

-- ============================================
-- Chat Sessions Table
-- ============================================
CREATE TABLE IF NOT EXISTS chat_sessions (
    id UUID NOT NULL PRIMARY KEY,
    tenant_id VARCHAR NOT NULL,
    session_id VARCHAR NOT NULL,
    channel_type VARCHAR,
    metadata JSON,
    created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
    updated_at TIMESTAMP WITHOUT TIME ZONE NOT NULL
);

-- ============================================
-- Chat Messages Table
-- ============================================
CREATE TABLE IF NOT EXISTS chat_messages (
    id UUID NOT NULL PRIMARY KEY,
    tenant_id VARCHAR NOT NULL,
    session_id VARCHAR NOT NULL,
    role VARCHAR NOT NULL,
    content TEXT NOT NULL,
    created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL
);

-- ============================================
-- Knowledge Bases Table
-- ============================================
CREATE TABLE IF NOT EXISTS knowledge_bases (
    id UUID NOT NULL PRIMARY KEY,
    tenant_id VARCHAR NOT NULL,
    name VARCHAR NOT NULL,
    description VARCHAR,
    created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
    updated_at TIMESTAMP WITHOUT TIME ZONE NOT NULL
);

-- ============================================
-- Documents Table
-- ============================================
CREATE TABLE IF NOT EXISTS documents (
    id UUID NOT NULL PRIMARY KEY,
    tenant_id VARCHAR NOT NULL,
    kb_id VARCHAR NOT NULL,
    file_name VARCHAR NOT NULL,
    file_path VARCHAR,
    file_size INTEGER,
    file_type VARCHAR,
    status VARCHAR NOT NULL DEFAULT 'pending',
    error_msg VARCHAR,
    created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
    updated_at TIMESTAMP WITHOUT TIME ZONE NOT NULL
);

-- ============================================
-- Index Jobs Table
-- ============================================
CREATE TABLE IF NOT EXISTS index_jobs (
    id UUID NOT NULL PRIMARY KEY,
    tenant_id VARCHAR NOT NULL,
    doc_id UUID NOT NULL,
    status VARCHAR NOT NULL DEFAULT 'pending',
    progress INTEGER NOT NULL DEFAULT 0,
    error_msg VARCHAR,
    created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
    updated_at TIMESTAMP WITHOUT TIME ZONE NOT NULL
);

-- ============================================
-- Indexes
-- ============================================

-- Chat Sessions Indexes
CREATE INDEX IF NOT EXISTS ix_chat_sessions_tenant_id ON chat_sessions (tenant_id);
CREATE UNIQUE INDEX IF NOT EXISTS ix_chat_sessions_tenant_session ON chat_sessions (tenant_id, session_id);

-- Chat Messages Indexes
CREATE INDEX IF NOT EXISTS ix_chat_messages_tenant_id ON chat_messages (tenant_id);
CREATE INDEX IF NOT EXISTS ix_chat_messages_tenant_session ON chat_messages (tenant_id, session_id);
CREATE INDEX IF NOT EXISTS ix_chat_messages_tenant_session_created ON chat_messages (tenant_id, session_id, created_at);

-- Knowledge Bases Indexes
CREATE INDEX IF NOT EXISTS ix_knowledge_bases_tenant_id ON knowledge_bases (tenant_id);

-- Documents Indexes
CREATE INDEX IF NOT EXISTS ix_documents_tenant_id ON documents (tenant_id);
CREATE INDEX IF NOT EXISTS ix_documents_tenant_kb ON documents (tenant_id, kb_id);
CREATE INDEX IF NOT EXISTS ix_documents_tenant_status ON documents (tenant_id, status);

-- Index Jobs Indexes
CREATE INDEX IF NOT EXISTS ix_index_jobs_tenant_id ON index_jobs (tenant_id);
CREATE INDEX IF NOT EXISTS ix_index_jobs_tenant_doc ON index_jobs (tenant_id, doc_id);
CREATE INDEX IF NOT EXISTS ix_index_jobs_tenant_status ON index_jobs (tenant_id, status);

-- ============================================
-- Verification
-- ============================================
-- Run this to verify all tables are created:
-- SELECT tablename FROM pg_tables WHERE schemaname = 'public' ORDER BY tablename;
