<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Compiler;

final class EventCompilerVerifier
{
    public function verify(string $basePath): array
    {
        $report = EventCompilerService::default()->compile($basePath)->toArray();
        $requiredArtifacts = ['eventManifest', 'listenerMetadata', 'attributeMetadata', 'dispatchTable'];
        $checks = [
            'stages' => count($report['stages'] ?? []) >= 4,
            'artifacts' => true,
            'eventManifest' => (($report['artifacts']['eventManifest']['count'] ?? 0) >= 31),
            'dispatchTable' => (($report['artifacts']['dispatchTable']['count'] ?? 0) >= 31),
            'warnings' => count($report['warnings'] ?? []) === 0,
        ];
        foreach ($requiredArtifacts as $artifact) {
            if (!isset($report['artifacts'][$artifact])) {
                $checks['artifacts'] = false;
            }
        }
        $issues = [];
        foreach ($checks as $check => $ok) {
            if (!$ok) {
                $issues[] = ['check' => $check, 'message' => 'Event compiler verification failed: ' . $check];
            }
        }
        return [
            'ok' => !in_array(false, $checks, true),
            'checks' => $checks,
            'issues' => $issues,
            'summary' => [
                'stages' => count($report['stages'] ?? []),
                'eventManifest' => $report['artifacts']['eventManifest']['count'] ?? 0,
                'dispatchTable' => $report['artifacts']['dispatchTable']['count'] ?? 0,
                'listenerMetadata' => $report['artifacts']['listenerMetadata']['summary']['listeners'] ?? 0,
                'attributeMetadata' => $report['artifacts']['attributeMetadata']['count'] ?? 0,
                'score' => in_array(false, $checks, true) ? 80 : 100,
            ],
        ];
    }
}
