<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Consumer\Hydration;

final class QueueConsumerStateValidator
{
    public function validate(array $state): array
    {
        $issues = [];

        $statistics = (array) ($state['statistics'] ?? []);
        $snapshot = (array) ($state['snapshot'] ?? []);
        $batchStatistics = (array) ($state['batchStatistics'] ?? []);

        if (!isset($statistics['consumed'], $statistics['acknowledged'], $statistics['failed'])) {
            $issues[] = ['check' => 'statistics', 'message' => 'Consumer statistics are incomplete.'];
        }

        if (!isset($snapshot['statistics'], $snapshot['queue'], $snapshot['pool'])) {
            $issues[] = ['check' => 'snapshot', 'message' => 'Consumer snapshot is incomplete.'];
        }

        if ($batchStatistics !== [] && !isset($batchStatistics['batches'], $batchStatistics['items'], $batchStatistics['acknowledged'])) {
            $issues[] = ['check' => 'batchStatistics', 'message' => 'Consumer batch statistics are incomplete.'];
        }

        return [
            'ok' => $issues === [],
            'issues' => $issues,
        ];
    }
}
