/**
 * MainAgent - Primary user-facing agent
 *
 * Implements DD §14.1 + state machine §20.1.
 * INV-1: no direct status writes (works via Scheduler/events).
 * INV-3: side effects only via ToolRegistry+PermissionEngine.
 *
 * @module packages/runtime/src/agents/main/MainAgent
 */

import type { SessionID, ProjectID } from '@aircoding/contracts'

export type MainAgentState =
  | 'IDLE'
  | 'CLASSIFYING'
  | 'ANSWERING'
  | 'DELEGATING'
  | 'DIRECT_MODE'
  | 'SCHEDULING'
  | 'ARCHITECTURE_DESIGNING'
  | 'CONFIRMING'
  | 'EXECUTING'
  | 'INTERRUPTING'
  | 'ARCHITECTURE_REVISING'
  | 'SUMMARIZING'

export interface MainAgentConfig {
  session_id: SessionID
  project_id: ProjectID
}

export class MainAgent {
  private config: MainAgentConfig
  state: MainAgentState = 'IDLE'

  constructor(config: MainAgentConfig) {
    this.config = config
  }

  /**
   * Handle incoming user message.
   * Classifies intent → routing decision.
   */
  async handle_user_message(message: string): Promise<{
    action: 'answer' | 'delegate' | 'direct'
    tasks?: string[]
    response?: string
  }> {
    // Classify intent
    this.state = 'CLASSIFYING'
    const classification = this.classify(message)

    switch (classification) {
      case 'simple_question':
      case 'clarification':
        this.state = 'ANSWERING'
        return { action: 'answer', response: 'Processing your question...' }

      case 'implementation_request':
      case 'task_request':
        this.state = 'DELEGATING'
        return { action: 'delegate', tasks: ['task-1'] }

      case 'direct_command':
        this.state = 'DIRECT_MODE'
        return { action: 'direct' }

      default:
        this.state = 'ANSWERING'
        return { action: 'answer', response: 'How can I help?' }
    }
  }

  /**
   * Classify user message intent.
   */
  private classify(message: string): string {
    const lower = message.toLowerCase()

    if (/^(what|how|why|when|where|who|can you|could you|explain)/.test(lower)) {
      return 'simple_question'
    }

    if (/^(implement|create|build|write|add|fix|change|update|remove|delete|refactor)/.test(lower)) {
      return 'implementation_request'
    }

    if (/^(run|execute|test|debug|check|inspect)/.test(lower)) {
      return 'direct_command'
    }

    return 'simple_question'
  }

  /**
   * Handle confirmation from user.
   */
  async handle_confirmation(confirmed: boolean): Promise<void> {
    if (this.state !== 'CONFIRMING') return

    if (confirmed) {
      this.state = 'DELEGATING'
    } else {
      this.state = 'IDLE'
    }
  }

  /**
   * Transition to idle after summarization.
   */
  summarize(): void {
    this.state = 'SUMMARIZING'
    // After summarization completes
    this.state = 'IDLE'
  }

  /**
   * Handle an interruption at the specified change level.
   * 'execution' → state EXECUTING
   * 'design'    → state ARCHITECTURE_REVISING
   * 'full'      → state ARCHITECTURE_DESIGNING
   */
  handle_interruption(change_level: 'execution' | 'design' | 'full'): void {
    this.state = 'INTERRUPTING'

    switch (change_level) {
      case 'execution':
        this.state = 'EXECUTING'
        break
      case 'design':
        this.state = 'ARCHITECTURE_REVISING'
        break
      case 'full':
        this.state = 'ARCHITECTURE_DESIGNING'
        break
    }
  }

  /**
   * Transition to CONFIRMING state (awaiting user confirmation).
   */
  transition_to_confirming(): void {
    this.state = 'CONFIRMING'
  }

  /**
   * Transition to EXECUTING state.
   */
  transition_to_executing(): void {
    this.state = 'EXECUTING'
  }

  /**
   * Transition to INTERRUPTING state.
   */
  transition_to_interrupting(): void {
    this.state = 'INTERRUPTING'
  }
}
