<?php
declare(strict_types=1);

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

use PpPhp\Core\Platform\Event\Streaming\Topic\Validation\StreamTopicVerifier;
use PpPhp\Core\Platform\Event\Streaming\Publisher\Validation\PublisherVerifier;
use PpPhp\Core\Platform\Event\Streaming\Subscriber\Validation\StreamSubscriberVerifier;
use PpPhp\Core\Platform\Event\Streaming\Broadcast\Validation\StreamBroadcastVerifier;
use PpPhp\Core\Platform\Event\Streaming\Replay\Validation\StreamPersistenceVerifier;
use PpPhp\Core\Platform\Event\Streaming\Dlq\Validation\StreamDlqVerifier;

final class EventStreamingRuntimeVerifier
{
    public function verify(string $basePath): array
    {
        $results = [
            'messageBus' => (new MessageBusVerifier())->verify($basePath),
            'topicRegistry' => (new StreamTopicVerifier())->verify($basePath),
            'publisherPipeline' => (new PublisherVerifier())->verify($basePath),
            'subscriberRuntime' => (new StreamSubscriberVerifier())->verify($basePath),
            'broadcastFanout' => (new StreamBroadcastVerifier())->verify($basePath),
            'streamPersistenceReplay' => (new StreamPersistenceVerifier())->verify($basePath),
            'dlqRetryPipeline' => (new StreamDlqVerifier())->verify($basePath),
        ];

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

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

            if ($ok) {
                $passed++;
            } else {
                $issues[] = [
                    'check' => $name,
                    'message' => 'Event streaming 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,
            ],
        ];
    }
}
