<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Distributed\Validation;

use PpPhp\Core\Platform\Event\Distributed\Lease\Validation\DistributedLeaseVerifier;
use PpPhp\Core\Platform\Event\Distributed\Queue\Validation\DistributedQueueVerifier;
use PpPhp\Core\Platform\Event\Distributed\Scheduler\Validation\DistributedSchedulerVerifier;
use PpPhp\Core\Platform\Event\Distributed\Failover\Validation\DistributedFailoverVerifier;

final class DistributedRuntimeFullVerifier
{
    public function verify(string $basePath): array
    {
        $results = [
            'workerCoordinator' => (new DistributedRuntimeVerifier())->verify($basePath),
            'jobLeasing' => (new DistributedLeaseVerifier())->verify($basePath),
            'queueCoordinator' => (new DistributedQueueVerifier())->verify($basePath),
            'scheduler' => (new DistributedSchedulerVerifier())->verify($basePath),
            'failover' => (new DistributedFailoverVerifier())->verify($basePath),
        ];

        $checks = [];
        $issues = [];
        $passed = 0;

        foreach ($results as $name => $result) {
            $ok = ($result['ok'] ?? false) === true;
            $checks[$name] = $ok;

            if ($ok) {
                $passed++;
                continue;
            }

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

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

        return [
            'ok' => $issues === [],
            'checks' => $checks,
            'results' => $results,
            'issues' => $issues,
            'summary' => [
                'checks' => $total,
                'passed' => $passed,
                'failed' => $total - $passed,
                'score' => $score,
            ],
        ];
    }
}
