<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Worker\Monitoring\Validation;

final class WorkerMonitoringVerifier
{
    public function verify(string $basePath): array
    {
        $checks = [
            'heartbeat' => (new WorkerHeartbeatVerifier())->verify($basePath),
            'health' => (new WorkerHealthVerifier())->verify($basePath),
            'stale' => (new WorkerStaleVerifier())->verify($basePath),
            'recovery' => (new WorkerRecoveryVerifier())->verify($basePath),
            'quarantine' => (new WorkerQuarantineVerifier())->verify($basePath),
        ];

        $issues = [];
        $passed = 0;

        foreach ($checks as $name => $result) {
            if (($result['ok'] ?? false) === true) {
                $passed++;
                continue;
            }

            $issues[] = [
                'check' => $name,
                'message' => 'Worker monitoring verification failed: ' . $name,
                'issues' => $result['issues'] ?? [],
            ];
        }

        $total = count($checks);
        $score = $total > 0 ? (int) round(($passed / $total) * 100) : 0;

        return [
            'ok' => $issues === [],
            'checks' => array_map(static fn (array $result): bool => ($result['ok'] ?? false) === true, $checks),
            'results' => $checks,
            'issues' => $issues,
            'summary' => [
                'checks' => $total,
                'passed' => $passed,
                'failed' => $total - $passed,
                'score' => $score,
            ],
        ];
    }
}
