<?php
declare(strict_types=1);

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

final class ProjectionRuntimeSnapshot
{
    public function __construct(
        private readonly array $definitions = [],
        private readonly array $readModels = [],
        private readonly array $checkpoints = [],
        private readonly array $statistics = [],
        private readonly string $createdAt = ''
    ) {}

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

    public function definitions(): array { return $this->definitions; }
    public function readModels(): array { return $this->readModels; }
    public function checkpoints(): array { return $this->checkpoints; }
    public function statistics(): array { return $this->statistics; }

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