<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Queue;

final class DeferredQueueVerifier
{
    public function verify(): array
    {
        $manager = new DeferredQueueManager();
        $id = $manager->enqueue('queue.verify', ['ok' => true]);
        $snapshot = $manager->snapshot();
        $flushed = $manager->flush(static function (DeferredEvent $event): void {});

        $checks = [
            'enqueue' => $id !== '',
            'snapshot' => count($snapshot->events()) === 1,
            'flush' => $flushed === 1,
            'clear' => $manager->statistics()->size() === 0,
            'drivers' => count($manager->summary()['drivers']) >= 2,
        ];

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

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