<?php
declare(strict_types=1);

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

final class WorkerHeartbeatSnapshot
{
    public function __construct(
        private readonly array $heartbeats = [],
        private readonly array $statistics = [],
        private readonly string $createdAt = ''
    ) {}

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

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

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