/**
 * ArchitectureDesigner - Architecture review gate
 *
 * Implements DD §14.2 + sequence §19.4.
 * INV-3: doc writes via ToolRegistry+PermissionEngine (no direct fs/shell).
 *
 * @module packages/runtime/src/agents/architecture/ArchitectureDesigner
 */

export type ArchitectureResult = 'silent_continue' | 'requires_user_confirmation' | 'requires_replan' | 'reject_or_escalate'

export interface ArchitectureImpact {
  result: ArchitectureResult
  affected_components: string[]
  change_summary: string
  risks: string[]
  requires_replan: boolean
}

export class ArchitectureDesigner {
  /**
   * Assess the architectural impact of a proposed change.
   */
  assess_impact(change: { description: string; files: string[] }): ArchitectureImpact {
    // Analyze which components are affected
    const affected = this.identify_affected_components(change.files)

    const impact: ArchitectureImpact = {
      result: 'silent_continue',
      affected_components: affected,
      change_summary: change.description,
      risks: [],
      requires_replan: false
    }

    // Classify by change scope, not mere package membership (DD §19.4):
    // - contract/interface change → user confirmation (breaking → escalate)
    // - broad multi-file change → replan
    // - otherwise → silent_continue
    const is_breaking = /deprecat|break|remove/.test(change.description.toLowerCase())
    const touches_contracts = affected.includes('contracts')
    const is_large = change.files.length > 10

    if (touches_contracts && is_breaking) {
      impact.result = 'reject_or_escalate'
      impact.risks.push('Breaking change to frozen contracts')
    } else if (touches_contracts) {
      impact.result = 'requires_user_confirmation'
      impact.risks.push('Contract/interface surface change')
    } else if (is_large) {
      impact.result = 'requires_replan'
      impact.requires_replan = true
      impact.risks.push('Large multi-file change requires re-planning')
    } else if (is_breaking) {
      impact.result = 'requires_user_confirmation'
      impact.risks.push('Potentially breaking change')
    }

    return impact
  }

  /**
   * Update architecture documentation (only if confirmed).
   * TODO(P7): Emit architecture.plan.updated event via EventIngestor.
   */
  async update_architecture_docs(impact: ArchitectureImpact): Promise<void> {
    // STUB: Only write docs if confirmed and impact is not reject
    if (impact.result === 'reject_or_escalate') return

    // INV-3: Uses ToolRegistry for file writes (not yet wired)
  }

  private identify_affected_components(files: string[]): string[] {
    const components: string[] = []
    for (const file of files) {
      if (file.includes('contracts')) components.push('contracts')
      if (file.includes('runtime')) components.push('runtime')
      if (file.includes('workers')) components.push('workers')
      if (file.includes('llm')) components.push('llm')
      if (file.includes('toolchain')) components.push('toolchain')
      if (file.includes('tui')) components.push('tui')
    }
    return [...new Set(components)]
  }
}
