<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Worker\Runtime;

final class WorkerRuntimeState
{
    public function __construct(
        private readonly array $workers = [],
        private readonly array $statistics = [],
        private readonly array $lifecycle = [],
        private readonly string $updatedAt = ''
    ) {}

    public static function fromArray(array $data): self
    {
        return new self((array) ($data['workers'] ?? []), (array) ($data['statistics'] ?? []), (array) ($data['lifecycle'] ?? []), (string) ($data['updatedAt'] ?? ''));
    }

    public function workers(): array { return $this->workers; }
    public function statistics(): array { return $this->statistics; }
    public function lifecycle(): array { return $this->lifecycle; }
    public function updatedAt(): string { return $this->updatedAt !== '' ? $this->updatedAt : date('c'); }

    public function toArray(): array
    {
        return ['workers' => $this->workers, 'statistics' => $this->statistics, 'lifecycle' => $this->lifecycle, 'updatedAt' => $this->updatedAt()];
    }
}
