<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Lifecycle;

final class LifecycleEventVerifier
{
    public function verify(string $basePath): array
    {
        $issues = [];
        $registry = LifecycleEventManifest::registry();
        $events = $registry->all();

        $checks = [
            'definitions' => count($events) >= 31,
            'categories' => count($registry->categories()) >= 5,
            'classes' => true,
            'compiler' => false,
            'duplicates' => count(array_unique(array_column($events, 'name'))) === count($events),
        ];

        foreach ($events as $event) {
            if (!class_exists($event['class'])) {
                $checks['classes'] = false;
                $issues[] = ['check' => 'classes', 'message' => 'Missing lifecycle event class.', 'class' => $event['class']];
            }
        }

        $compiled = (new LifecycleEventCompiler())->compile($basePath);
        $checks['compiler'] = ($compiled['count'] ?? 0) === count($events);

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

        return [
            'ok' => !in_array(false, $checks, true),
            'checks' => $checks,
            'issues' => $issues,
            'summary' => [
                'events' => count($events),
                'categories' => count($registry->categories()),
                'score' => in_array(false, $checks, true) ? 80 : 100,
            ],
        ];
    }
}
