<?php
declare(strict_types=1);

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

final class CommandRuntimeSnapshot
{
    public function __construct(
        private readonly array $handlers = [],
        private readonly array $commands = [],
        private readonly array $results = [],
        private readonly array $statistics = [],
        private readonly string $createdAt = ''
    ) {}

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

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

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