<?php

declare(strict_types=1);

namespace App\Framework\Http\Exceptions;

use App\Framework\Exception\ExceptionContext;
use App\Framework\Exception\FrameworkException;

/**
 * Exception thrown when a middleware exceeds its execution time limit
 */
final class MiddlewareTimeoutException extends FrameworkException
{
    public function __construct(
        string $middlewareName,
        float $timeout,
        int $code = 0,
        ?\Throwable $previous = null
    ) {
        $message = sprintf(
            'Middleware "%s" exceeded its execution time limit of %.2f seconds',
            $middlewareName,
            $timeout
        );

        $context = ExceptionContext::empty()
            ->withData([
                'middleware_name' => $middlewareName,
                'timeout' => $timeout,
                'error_type' => 'middleware_timeout',
            ]);

        parent::__construct($message, $context, $code, $previous);
    }
}
