<?php
declare(strict_types=1);

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

final class QueueConsumerBatchStatistics
{
    public function __construct(
        private readonly int $batches = 0,
        private readonly int $items = 0,
        private readonly int $acknowledged = 0,
        private readonly int $failed = 0,
        private readonly int $partial = 0
    ) {}

    public function record(QueueConsumerBatchResult $result): self
    {
        return new self(
            $this->batches + 1,
            $this->items + $result->total(),
            $this->acknowledged + $result->acknowledged(),
            $this->failed + $result->failed(),
            $this->partial + ($result->partial() ? 1 : 0)
        );
    }

    public function toArray(): array
    {
        return [
            'batches' => $this->batches,
            'items' => $this->items,
            'acknowledged' => $this->acknowledged,
            'failed' => $this->failed,
            'partial' => $this->partial,
            'successRate' => $this->items > 0 ? round(($this->acknowledged / $this->items) * 100, 2) : 0,
        ];
    }
}
