<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Workflow;

final class WorkflowRuntimeSnapshot
{
    public function __construct(
        private readonly array $definitions = [],
        private readonly array $instances = [],
        private readonly array $tasks = [],
        private readonly array $executions = [],
        private readonly array $statistics = [],
        private readonly string $createdAt = ''
    ) {}

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

    public function definitions(): array { return $this->definitions; }
    public function instances(): array { return $this->instances; }
    public function tasks(): array { return $this->tasks; }
    public function executions(): array { return $this->executions; }
    public function statistics(): array { return $this->statistics; }

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