<?php

namespace App\Console\Commands;

use App\Domain\Agent\Actions\HealthCheckAction;
use App\Domain\Agent\Actions\UpdateAgentRiskProfileAction;
use App\Domain\Agent\Enums\AgentStatus;
use App\Domain\Agent\Models\Agent;
use App\Domain\Agent\Services\EquivocationDetector;
use Illuminate\Console\Command;

class AgentHealthCheck extends Command
{
    protected $signature = 'agents:health-check';

    protected $description = 'Run health checks against all active AI agents';

    public function handle(
        HealthCheckAction $action,
        UpdateAgentRiskProfileAction $riskAction,
        EquivocationDetector $equivocationDetector,
    ): int {
        $agents = Agent::withoutGlobalScopes()->whereIn('status', [AgentStatus::Active, AgentStatus::Degraded])->get();

        if ($agents->isEmpty()) {
            $this->info('No active agents to check.');

            return self::SUCCESS;
        }

        $passed = 0;
        $failed = 0;
        $equivocators = 0;

        foreach ($agents as $agent) {
            $result = $action->execute($agent);

            if ($result) {
                $passed++;
                $this->line("  [OK] {$agent->name} ({$agent->provider}/{$agent->model})");
            } else {
                $failed++;
                $this->warn("  [FAIL] {$agent->name} ({$agent->provider}/{$agent->model})");
            }

            // Report equivocation status
            $count = $agent->fresh()->equivocation_count ?? 0;
            if ($count > 0) {
                $equivocators++;
                $this->warn("  [EQUIVOC] {$agent->name} has {$count} equivocation(s) in the last 24h");
            }

            // Compute risk profile for every checked agent
            try {
                $riskAction->execute($agent->fresh());
            } catch (\Throwable $e) {
                $this->warn("  [RISK] Failed to update risk profile for {$agent->name}: {$e->getMessage()}");
            }
        }

        $this->info("Health check complete: {$passed} passed, {$failed} failed, {$equivocators} equivocating.");

        return $failed > 0 ? self::FAILURE : self::SUCCESS;
    }
}
