<?php

declare(strict_types=1);

namespace App\Framework\RateLimit;

readonly class RateLimitResult
{
    public function __construct(
        private bool $allowed,
        private int $limit,
        private int $current,
        private ?int $retryAfter = null
    ) {}

    public static function allowed(int $limit, int $current): self
    {
        return new self(
            allowed: true,
            limit: $limit,
            current: $current
        );
    }

    public static function exceeded(int $limit, int $current, int $retryAfter): self
    {
        return new self(
            allowed: false,
            limit: $limit,
            current: $current,
            retryAfter: $retryAfter
        );
    }

    public function isAllowed(): bool
    {
        return $this->allowed;
    }

    public function isExceeded(): bool
    {
        return !$this->allowed;
    }

    public function getLimit(): int
    {
        return $this->limit;
    }

    public function getCurrent(): int
    {
        return $this->current;
    }

    public function getRetryAfter(): ?int
    {
        return $this->retryAfter;
    }

    public function getRemainingRequests(): int
    {
        return max(0, $this->limit - $this->current);
    }
}
