<?php

declare(strict_types=1);

namespace App\Framework\LiveComponents\Exceptions;

use App\Framework\Exception\ErrorCode;
use App\Framework\Exception\FrameworkException;

/**
 * Rate Limit Exceeded Exception
 *
 * Thrown when a LiveComponent action exceeds rate limits.
 * Includes retry-after information for client handling.
 */
final class RateLimitExceededException extends FrameworkException
{
    public static function forAction(
        string $componentName,
        string $action,
        int $limit,
        int $current,
        int $retryAfter
    ): self {
        return self::create(
            ErrorCode::HTTP_RATE_LIMIT_EXCEEDED,
            "Rate limit exceeded for action '{$action}' on component '{$componentName}'"
        )->withData([
            'component' => $componentName,
            'action' => $action,
            'limit' => $limit,
            'current' => $current,
            'retry_after_seconds' => $retryAfter,
        ])->withRetryAfter($retryAfter);
    }

    public static function forComponent(
        string $componentName,
        int $limit,
        int $current,
        int $retryAfter
    ): self {
        return self::create(
            ErrorCode::HTTP_RATE_LIMIT_EXCEEDED,
            "Rate limit exceeded for component '{$componentName}'"
        )->withData([
            'component' => $componentName,
            'limit' => $limit,
            'current' => $current,
            'retry_after_seconds' => $retryAfter,
        ])->withRetryAfter($retryAfter);
    }
}
