<?php

declare(strict_types=1);

namespace PhpWA\Runtime;

use InvalidArgumentException;
use PhpWA\Client\SessionEvent;

final class WorkerRunResult
{
    /** @var bool */ private $lockAcquired;
    /** @var int */ private $nodes;
    /** @var int */ private $replayed;
    /** @var string */ private $state;
    /** @var list<SessionEvent> */ private $events;

    /** @param list<SessionEvent> $events */
    public function __construct(bool $lockAcquired, int $nodes, int $replayed, string $state, array $events)
    {
        if ($nodes < 0 || $replayed < 0 || $state === '') { throw new InvalidArgumentException('Worker run result is invalid.'); }
        foreach ($events as $event) { if (!$event instanceof SessionEvent) { throw new InvalidArgumentException('Worker run events are invalid.'); } }
        $this->lockAcquired = $lockAcquired; $this->nodes = $nodes; $this->replayed = $replayed;
        $this->state = $state; $this->events = array_values($events);
    }

    public static function busy(): self { return new self(false, 0, 0, 'busy', []); }
    public function lockAcquired(): bool { return $this->lockAcquired; }
    public function nodesProcessed(): int { return $this->nodes; }
    public function stanzasReplayed(): int { return $this->replayed; }
    public function state(): string { return $this->state; }
    /** @return list<SessionEvent> */ public function events(): array { return $this->events; }
}
