<?php
declare(strict_types=1);

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

final class DistributedFailoverAction
{
    public function __construct(
        private readonly string $action,
        private readonly string $target = '',
        private readonly bool $successful = true,
        private readonly string $reason = '',
        private readonly string $performedAt = ''
    ) {}

    public static function nodeRecovered(string $nodeId): self
    {
        return new self('node_recovered', $nodeId, true, 'failed node processed for recovery', date('c'));
    }

    public static function leaseRecovered(int $count): self
    {
        return new self('lease_recovered', 'leases', true, 'leases recovered: ' . $count, date('c'));
    }

    public static function queueRebalanced(int $moves): self
    {
        return new self('queue_rebalanced', 'queues', true, 'queue moves: ' . $moves, date('c'));
    }

    public function toArray(): array
    {
        return [
            'action' => $this->action,
            'target' => $this->target,
            'successful' => $this->successful,
            'reason' => $this->reason,
            'performedAt' => $this->performedAt !== '' ? $this->performedAt : date('c'),
        ];
    }
}
