export interface AgentStep {
  name: string;
  description: string;
  query: string | ((inputData: any) => string | Promise<string>);
  enabled: boolean;
  maxRetries: number;
  timeoutMs: number;
  validation?: (result: any) => boolean;
  retryStrategy?: {
    maxRetries: number;
    delayMs: number;
  };
  maxTokens?: number;
  temperature?: number;
}

export interface RetryStrategy {
  maxRetries: number;
  delayMs: number;
  backoffMultiplier?: number;
}

export interface AgentExecution {
  id: string;
  documentId: string;
  sessionId: string;
  agentName: string;
  stepNumber: number;
  status: 'pending' | 'processing' | 'completed' | 'failed';
  inputData?: any;
  outputData?: any;
  validationResult?: any;
  processingTimeMs?: number;
  errorMessage?: string;
  retryCount: number;
  createdAt: Date;
  updatedAt: Date;
}

export interface AgenticRAGSession {
  id: string;
  documentId: string;
  userId: string;
  strategy: 'agentic_rag' | 'chunking' | 'rag';
  status: 'pending' | 'processing' | 'completed' | 'failed';
  totalAgents: number;
  completedAgents: number;
  failedAgents: number;
  overallValidationScore?: number;
  processingTimeMs?: number;
  apiCallsCount: number;
  totalCost?: number;
  reasoningSteps: AgentExecution[];
  finalResult?: any;
  createdAt: Date;
  completedAt: Date | undefined;
}

export interface QualityMetrics {
  id: string;
  documentId: string;
  sessionId: string;
  metricType: 'completeness' | 'accuracy' | 'consistency' | 'relevance';
  metricValue: number;
  metricDetails: any;
  createdAt: Date;
}

export interface AgenticRAGResult {
  success: boolean;
  summary: string;
  analysisData: any; // CIMReview type
  reasoningSteps: AgentExecution[];
  processingTime: number;
  apiCalls: number;
  totalCost: number;
  qualityMetrics: QualityMetrics[];
  sessionId: string;
  error?: string | undefined;
}

export interface PerformanceReport {
  averageProcessingTime: number;
  p95ProcessingTime: number;
  averageApiCalls: number;
  averageCost: number;
  successRate: number;
  averageQualityScore: number;
}

export interface AgenticRAGHealthStatus {
  status: 'healthy' | 'degraded' | 'unhealthy';
  agents: {
    [agentName: string]: {
      status: 'healthy' | 'degraded' | 'unhealthy';
      lastExecutionTime?: number;
      successRate: number;
      averageProcessingTime: number;
    };
  };
  overall: {
    successRate: number;
    averageProcessingTime: number;
    activeSessions: number;
    errorRate: number;
  };
  timestamp: Date;
}

export interface CircuitBreakerState {
  state: 'CLOSED' | 'OPEN' | 'HALF_OPEN';
  failures: number;
  lastFailureTime: number;
  failureThreshold: number;
  timeoutMs: number;
}

export interface FallbackStrategy {
  type: 'reduced_agents' | 'simplified_analysis' | 'cached_result';
  description: string;
  enabled: boolean;
}

export interface AgenticRAGError {
  type: 'AGENT_EXECUTION_FAILED' | 'VALIDATION_FAILED' | 'TIMEOUT_ERROR' | 'RATE_LIMIT_ERROR' | 'INVALID_RESPONSE' | 'DATABASE_ERROR' | 'CONFIGURATION_ERROR';
  message: string;
  agentName?: string;
  retryable: boolean;
  context?: any;
  timestamp: Date;
}

export interface AgenticRAGConfig {
  enabled: boolean;
  maxAgents: number;
  parallelProcessing: boolean;
  validationStrict: boolean;
  retryAttempts: number;
  timeoutPerAgent: number;
  qualityThreshold: number;
  completenessThreshold: number;
  consistencyCheck: boolean;
  detailedLogging: boolean;
  performanceTracking: boolean;
  errorReporting: boolean;
}

export interface AgentRegistry {
  [agentName: string]: AgentStep;
}

export interface AgentExecutionRequest {
  sessionId: string;
  agentName: string;
  inputData: any;
  priority?: 'high' | 'normal' | 'low';
  timeoutMs?: number;
}

export interface AgentExecutionResponse {
  success: boolean;
  execution: AgentExecution;
  error?: string;
}

export interface QualityAssessmentResult {
  completeness: { score: number; details: any };
  consistency: { score: number; details: any };
  accuracy: { score: number; details: any };
  relevance: { score: number; details: any };
  overall: number;
}

export interface SessionMetrics {
  sessionId: string;
  documentId: string;
  userId: string;
  startTime: Date;
  endTime?: Date;
  totalProcessingTime: number;
  agentExecutions: AgentExecution[];
  qualityMetrics: QualityMetrics[];
  apiCalls: number;
  totalCost: number;
  success: boolean;
  error?: string;
} 