<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Streaming;

final class MessageBusSnapshot
{
    public function __construct(
        private readonly array $topics = [],
        private readonly array $messages = [],
        private readonly array $statistics = [],
        private readonly string $createdAt = ''
    ) {}

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

    public function topics(): array { return $this->topics; }
    public function messages(): array { return $this->messages; }
    public function statistics(): array { return $this->statistics; }

    public function toArray(): array
    {
        return [
            'topics' => $this->topics,
            'messages' => $this->messages,
            'statistics' => $this->statistics,
            'createdAt' => $this->createdAt !== '' ? $this->createdAt : date('c'),
        ];
    }
}
