/**
 * ReviewerRole - Code review worker
 * Read-only, reviews code changes for correctness and compliance.
 *
 * @module packages/workers/src/roles/ReviewerRole
 */

import { WorkerRuntime } from '../WorkerRuntime.js'

export interface ReviewerResult {
  status: 'pass' | 'fail' | 'needs_work' | 'blocked'
  findings: Array<{
    severity: 'info' | 'warning' | 'error' | 'fatal'
    file?: string
    line?: number
    message: string
    suggestion?: string
  }>
  summary: string
}

export class ReviewerRole {
  private runtime: WorkerRuntime

  constructor(runtime: WorkerRuntime) {
    this.runtime = runtime
  }

  async run(review_spec: { task_id: string; change_files: string[] }): Promise<ReviewerResult> {
    const result: ReviewerResult = { status: 'pass', findings: [], summary: '' }

    try {
      this.runtime.emit('review.started', { task_id: review_spec.task_id })

      for (const file of review_spec.change_files) {
        // Read each changed file
        const read_result = await this.runtime.call_tool('fs.read', { path: file })

        // Get git diff
        const diff_result = await this.runtime.call_tool('git.diff', { path: file })

        // REVIEW CHECKS (INV-1..5 compliance):

        // INV-1: Check for direct status writes
        // INV-3: Check for direct side effects
        // INV-4: Check import direction
        // Style/convention checks

        // Stub findings
        result.findings.push({
          severity: 'info',
          file,
          message: 'Review stub — file inspected',
          suggestion: 'Full review implementation in progress'
        })
      }

      result.status = 'pass'
      result.summary = `Reviewed ${review_spec.change_files.length} files`

      this.runtime.checkpoint('review_completed', { task_id: review_spec.task_id })
      return result

    } catch (error) {
      result.status = 'blocked'
      result.findings.push({ severity: 'fatal', message: error instanceof Error ? error.message : String(error) })
      return result
    }
  }
}
