<?php

declare(strict_types=1);

namespace PhpWA\Client;

use InvalidArgumentException;

final class PreKeyUploadResult
{
    /** @var bool */ private $hadJob;
    /** @var int */ private $uploaded;
    /** @var int */ private $firstUploadedId;
    /** @var int */ private $lastUploadedId;
    public function __construct(bool $hadJob, int $uploaded, int $firstUploadedId, int $lastUploadedId)
    {
        if ($uploaded < 0 || $firstUploadedId < 0 || $lastUploadedId < 0
            || (!$hadJob && ($uploaded !== 0 || $firstUploadedId !== 0 || $lastUploadedId !== 0))
            || ($hadJob && ($uploaded < 1 || $lastUploadedId - $firstUploadedId + 1 !== $uploaded))) {
            throw new InvalidArgumentException('Pre-key upload result is invalid.');
        }
        $this->hadJob = $hadJob; $this->uploaded = $uploaded;
        $this->firstUploadedId = $firstUploadedId; $this->lastUploadedId = $lastUploadedId;
    }
    public static function idle(): self { return new self(false, 0, 0, 0); }
    public function hadJob(): bool { return $this->hadJob; }
    public function uploaded(): int { return $this->uploaded; }
    public function firstUploadedId(): int { return $this->firstUploadedId; }
    public function lastUploadedId(): int { return $this->lastUploadedId; }
}
