<?php
declare(strict_types=1);

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

final class EventVersioningSnapshot
{
    public function __construct(private readonly array $versions = [], private readonly array $upcasters = [], private readonly array $results = [], private readonly array $statistics = [], private readonly string $createdAt = '') {}

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

    public function versions(): array { return $this->versions; }
    public function upcasters(): array { return $this->upcasters; }
    public function results(): array { return $this->results; }
    public function statistics(): array { return $this->statistics; }

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