<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Dispatcher\Runtime;

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

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

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

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