<?php
declare(strict_types=1);

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

final class DistributedFailoverReport
{
    public function __construct(
        private readonly array $nodes = [],
        private readonly array $actions = [],
        private readonly array $leaseRecovery = [],
        private readonly array $queueRecovery = [],
        private readonly array $statistics = [],
        private readonly array $policy = [],
        private readonly string $createdAt = ''
    ) {}

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

    public function nodes(): array { return $this->nodes; }
    public function actions(): array { return $this->actions; }
    public function statistics(): array { return $this->statistics; }

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