<?php

declare(strict_types=1);

namespace PhpWA\Runtime;

use InvalidArgumentException;

final class AppStateJobRunResult
{
    /** @var int */ private $jobs;
    /** @var int */ private $pages;
    /** @var int */ private $mutations;
    /** @var int */ private $failed;
    /** @var int */ private $deadLettered;
    /** @var string|null */ private $lastError;

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

    public function jobs(): int { return $this->jobs; }
    public function pages(): int { return $this->pages; }
    public function mutations(): int { return $this->mutations; }
    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; }
}
