<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Consumer;

final class QueueConsumerRuntimeState
{
    public function __construct(
        private readonly array $statistics = [],
        private readonly array $snapshot = [],
        private readonly array $batchStatistics = [],
        private readonly string $updatedAt = ''
    ) {}

    public static function fromArray(array $data): self
    {
        return new self(
            (array) ($data['statistics'] ?? []),
            (array) ($data['snapshot'] ?? []),
            (array) ($data['batchStatistics'] ?? []),
            (string) ($data['updatedAt'] ?? '')
        );
    }

    public function statistics(): array { return $this->statistics; }
    public function snapshot(): array { return $this->snapshot; }
    public function batchStatistics(): array { return $this->batchStatistics; }
    public function updatedAt(): string { return $this->updatedAt !== '' ? $this->updatedAt : date('c'); }

    public function toArray(): array
    {
        return [
            'statistics' => $this->statistics,
            'snapshot' => $this->snapshot,
            'batchStatistics' => $this->batchStatistics,
            'updatedAt' => $this->updatedAt(),
        ];
    }
}
