<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Worker;

final class WorkerStatus
{
    public const IDLE = 'idle';
    public const RUNNING = 'running';
    public const STOPPED = 'stopped';
    public const FAILED = 'failed';

    public function __construct(private readonly string $state = self::IDLE, private readonly string $updatedAt = '') {}

    public static function idle(): self { return new self(self::IDLE); }
    public static function running(): self { return new self(self::RUNNING); }
    public static function stopped(): self { return new self(self::STOPPED); }
    public static function failed(): self { return new self(self::FAILED); }

    public function state(): string { return $this->state; }
    public function updatedAt(): string { return $this->updatedAt !== '' ? $this->updatedAt : date('c'); }

    public function isRunning(): bool { return $this->state === self::RUNNING; }

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