<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Console\Commands;

use PpPhp\Core\Platform\Console\AbstractCommand;
use PpPhp\Core\Platform\Console\ConsolePath;
use PpPhp\Core\Platform\Event\Consumer\QueueConsumerManager;

final class WorkerConsumerStatsCommand extends AbstractCommand
{
    public function name(): string { return 'worker:consumer:stats'; }
    public function description(): string { return 'PPphp Queue Consumer Stats'; }

    public function handle(array $arguments = []): int
    {
        $this->title('PPphp Queue Consumer Stats');

        $manager = QueueConsumerManager::instance(ConsolePath::basePath());
        $state = $manager->consoleState();

        foreach ($state->statistics() as $key => $value) {
            $this->line($key . ': ' . $value);
        }

        $batch = $state->batchStatistics();
        if (($batch['batches'] ?? 0) > 0) {
            $this->line('batchBatches: ' . $batch['batches']);
            $this->line('batchItems: ' . $batch['items']);
        }

        $validation = $state->validation();
        $this->line('hydrated: ' . ($state->persisted() ? 'YES' : 'NO'));
        $this->line('stateValid: ' . (($validation['ok'] ?? false) ? 'YES' : 'NO'));

        return 0;
    }
}
