<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Distributed\Scheduler;

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

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

    public function assignments(): array { return $this->assignments; }
    public function statistics(): array { return $this->statistics; }
    public function policy(): array { return $this->policy; }

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