-- Migration: Create Agentic RAG Tables
-- Description: Creates tables for agentic RAG processing, session management, and quality metrics

-- Agent execution tracking
CREATE TABLE agent_executions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    document_id UUID REFERENCES documents(id) ON DELETE CASCADE,
    session_id UUID, -- Will reference agentic_rag_sessions(id) after table creation
    agent_name VARCHAR(100) NOT NULL,
    step_number INTEGER NOT NULL,
    status VARCHAR(50) NOT NULL CHECK (status IN ('pending', 'processing', 'completed', 'failed')),
    input_data JSONB,
    output_data JSONB,
    validation_result JSONB,
    processing_time_ms INTEGER,
    error_message TEXT,
    retry_count INTEGER DEFAULT 0,
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);

-- Agentic RAG processing sessions
CREATE TABLE agentic_rag_sessions (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    document_id UUID REFERENCES documents(id) ON DELETE CASCADE,
    user_id UUID REFERENCES users(id) ON DELETE CASCADE,
    strategy VARCHAR(50) NOT NULL CHECK (strategy IN ('agentic_rag', 'chunking', 'rag')),
    status VARCHAR(50) NOT NULL CHECK (status IN ('pending', 'processing', 'completed', 'failed')),
    total_agents INTEGER NOT NULL,
    completed_agents INTEGER DEFAULT 0,
    failed_agents INTEGER DEFAULT 0,
    overall_validation_score DECIMAL(3,2),
    processing_time_ms INTEGER,
    api_calls_count INTEGER DEFAULT 0,
    total_cost DECIMAL(10,4),
    reasoning_steps JSONB,
    final_result JSONB,
    created_at TIMESTAMP DEFAULT NOW(),
    completed_at TIMESTAMP
);

-- Quality metrics tracking
CREATE TABLE processing_quality_metrics (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    document_id UUID REFERENCES documents(id) ON DELETE CASCADE,
    session_id UUID REFERENCES agentic_rag_sessions(id) ON DELETE CASCADE,
    metric_type VARCHAR(100) NOT NULL CHECK (metric_type IN ('completeness', 'accuracy', 'consistency', 'relevance')),
    metric_value DECIMAL(3,2) NOT NULL CHECK (metric_value >= 0 AND metric_value <= 1),
    metric_details JSONB,
    created_at TIMESTAMP DEFAULT NOW()
);

-- Add foreign key constraint for agent_executions.session_id
ALTER TABLE agent_executions 
ADD CONSTRAINT fk_agent_executions_session_id 
FOREIGN KEY (session_id) REFERENCES agentic_rag_sessions(id) ON DELETE CASCADE;

-- Create indexes for better performance
CREATE INDEX idx_agent_executions_document_id ON agent_executions(document_id);
CREATE INDEX idx_agent_executions_session_id ON agent_executions(session_id);
CREATE INDEX idx_agent_executions_agent_name ON agent_executions(agent_name);
CREATE INDEX idx_agent_executions_status ON agent_executions(status);
CREATE INDEX idx_agent_executions_created_at ON agent_executions(created_at);

CREATE INDEX idx_agentic_rag_sessions_document_id ON agentic_rag_sessions(document_id);
CREATE INDEX idx_agentic_rag_sessions_user_id ON agentic_rag_sessions(user_id);
CREATE INDEX idx_agentic_rag_sessions_status ON agentic_rag_sessions(status);
CREATE INDEX idx_agentic_rag_sessions_created_at ON agentic_rag_sessions(created_at);

CREATE INDEX idx_processing_quality_metrics_document_id ON processing_quality_metrics(document_id);
CREATE INDEX idx_processing_quality_metrics_session_id ON processing_quality_metrics(session_id);
CREATE INDEX idx_processing_quality_metrics_metric_type ON processing_quality_metrics(metric_type);
CREATE INDEX idx_processing_quality_metrics_created_at ON processing_quality_metrics(created_at);

-- Add updated_at trigger function if it doesn't exist
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = NOW();
    RETURN NEW;
END;
$$ language 'plpgsql';

-- Create triggers for updated_at
CREATE TRIGGER update_agent_executions_updated_at 
    BEFORE UPDATE ON agent_executions 
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

-- Add comments for documentation
COMMENT ON TABLE agent_executions IS 'Tracks individual agent execution steps within agentic RAG sessions';
COMMENT ON TABLE agentic_rag_sessions IS 'Manages agentic RAG processing sessions and their overall status';
COMMENT ON TABLE processing_quality_metrics IS 'Stores quality assessment metrics for processed documents'; 