<?php

declare(strict_types=1);

namespace BotGuard\Core\Exceptions;

use RuntimeException;

/**
 * Represents an HTTP error condition (404, 405, 403, 500, ...) that the Kernel
 * translates into a proper error response instead of a stack trace.
 */
final class HttpException extends RuntimeException
{
    public function __construct(
        private readonly int $statusCode,
        string $message = '',
        ?\Throwable $previous = null,
    ) {
        parent::__construct($message !== '' ? $message : self::defaultMessage($statusCode), $statusCode, $previous);
    }

    public function getStatusCode(): int
    {
        return $this->statusCode;
    }

    private static function defaultMessage(int $status): string
    {
        return match ($status) {
            400 => 'Bad Request',
            401 => 'Unauthorized',
            403 => 'Forbidden',
            404 => 'Not Found',
            405 => 'Method Not Allowed',
            419 => 'Page Expired',
            429 => 'Too Many Requests',
            default => 'Server Error',
        };
    }
}
