<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Dispatcher;

final class DispatcherResult
{
    public function __construct(
        private readonly bool $ok,
        private readonly string $eventId,
        private readonly string $eventName,
        private readonly bool $retried = false,
        private readonly bool $deadLettered = false,
        private readonly string $message = ''
    ) {}

    public static function success(string $eventId, string $eventName): self
    {
        return new self(true, $eventId, $eventName);
    }

    public static function retry(string $eventId, string $eventName, string $message = ''): self
    {
        return new self(false, $eventId, $eventName, true, false, $message);
    }

    public static function deadLetter(string $eventId, string $eventName, string $message = ''): self
    {
        return new self(false, $eventId, $eventName, false, true, $message);
    }

    public function ok(): bool { return $this->ok; }
    public function retried(): bool { return $this->retried; }
    public function deadLettered(): bool { return $this->deadLettered; }

    public function toArray(): array
    {
        return [
            'ok' => $this->ok,
            'eventId' => $this->eventId,
            'eventName' => $this->eventName,
            'retried' => $this->retried,
            'deadLettered' => $this->deadLettered,
            'message' => $this->message,
        ];
    }
}
