<?php

declare(strict_types=1);

namespace PhpWA\Runtime;

use InvalidArgumentException;

final class OutboxRunResult
{
    /** @var int */ private $delivered;
    /** @var int */ private $failed;
    /** @var int */ private $deadLettered;
    /** @var string|null */ private $lastError;

    public function __construct(int $delivered, int $failed, int $deadLettered, ?string $lastError)
    {
        if ($delivered < 0 || $failed < 0 || $deadLettered < 0
            || ($lastError !== null && preg_match('/\A[a-z][a-z0-9_]{0,63}\z/D', $lastError) !== 1)) {
            throw new InvalidArgumentException('Outbox run result is invalid.');
        }
        $this->delivered = $delivered; $this->failed = $failed;
        $this->deadLettered = $deadLettered; $this->lastError = $lastError;
    }

    public function delivered(): int { return $this->delivered; }
    public function failed(): int { return $this->failed; }
    public function deadLettered(): int { return $this->deadLettered; }
    /** Stable secret-free code, never the original exception message. */
    public function lastErrorCode(): ?string { return $this->lastError; }
    /** @deprecated Use lastErrorCode(); retained for the alpha API. */
    public function lastError(): ?string { return $this->lastError; }
}
