<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Sourcing\Domain;

final class DomainEventRuntimeSnapshot
{
    public function __construct(
        private readonly array $listeners = [],
        private readonly array $envelopes = [],
        private readonly array $results = [],
        private readonly array $checkpoint = [],
        private readonly array $statistics = [],
        private readonly string $createdAt = ''
    ) {}

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

    public function listeners(): array { return $this->listeners; }
    public function envelopes(): array { return $this->envelopes; }
    public function results(): array { return $this->results; }
    public function checkpoint(): array { return $this->checkpoint; }
    public function statistics(): array { return $this->statistics; }

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