<?php
declare(strict_types=1);

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

use PpPhp\Core\Platform\Event\Worker\Monitoring\WorkerHeartbeatCollection;
use PpPhp\Core\Platform\Event\Worker\Monitoring\WorkerHeartbeat;
use PpPhp\Core\Platform\Event\Worker\Monitoring\WorkerStaleManager;
use PpPhp\Core\Platform\Event\Worker\Monitoring\WorkerStalePolicy;

final class WorkerStaleVerifier
{
    public function verify(string $basePath): array
    {
        $collection = WorkerHeartbeatCollection::fromArray([
            [
                'workerId' => 'worker_stale_healthy',
                'status' => 'idle',
                'handled' => 1,
                'failed' => 0,
                'lastEvent' => 'healthy.event',
                'recordedAt' => date('c'),
            ],
            [
                'workerId' => 'worker_stale_timeout',
                'status' => 'idle',
                'handled' => 2,
                'failed' => 0,
                'lastEvent' => 'stale.event',
                'recordedAt' => date('c', time() - 120),
            ],
            [
                'workerId' => 'worker_stale_hung',
                'status' => 'running',
                'handled' => 3,
                'failed' => 0,
                'lastEvent' => 'hung.event',
                'recordedAt' => date('c', time() - 120),
            ],
            [
                'workerId' => 'worker_stale_failed',
                'status' => 'failed',
                'handled' => 1,
                'failed' => 1,
                'lastEvent' => 'failed.event',
                'recordedAt' => date('c'),
            ],
        ]);

        $manager = WorkerStaleManager::resetInstance($basePath, new WorkerStalePolicy(30, 60, 300));
        $manager->clear();

        $report = $manager->inspect($collection, ['worker_stale_missing']);
        $loaded = $manager->load();
        $stats = $loaded->statistics();

        $checks = [
            'inspection' => count($report->records()) === 5,
            'healthy' => ($stats['healthy'] ?? 0) === 1,
            'stale' => ($stats['stale'] ?? 0) === 1,
            'hung' => ($stats['hung'] ?? 0) === 1,
            'failed' => ($stats['failed'] ?? 0) === 1,
            'offline' => ($stats['offline'] ?? 0) === 1,
            'persistence' => $manager->store()->exists(),
        ];

        $issues = [];
        foreach ($checks as $check => $ok) {
            if (!$ok) {
                $issues[] = ['check' => $check, 'message' => 'Worker stale verification failed: ' . $check];
            }
        }

        return [
            'ok' => !in_array(false, $checks, true),
            'checks' => $checks,
            'issues' => $issues,
            'summary' => [
                'workers' => $stats['workers'] ?? 0,
                'healthy' => $stats['healthy'] ?? 0,
                'stale' => $stats['stale'] ?? 0,
                'hung' => $stats['hung'] ?? 0,
                'offline' => $stats['offline'] ?? 0,
                'failed' => $stats['failed'] ?? 0,
                'score' => in_array(false, $checks, true) ? 80 : 100,
            ],
        ];
    }
}
