<?php

declare(strict_types=1);

namespace App\Exception;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\WithHttpStatus;

#[WithHttpStatus(Response::HTTP_TOO_MANY_REQUESTS, headers: [
    'x-error-code' => 'submission_rate_limited',
])]
final class SubmissionRateLimitedException extends \DomainException
{
    public function __construct(?\DateTimeInterface $retryAfter)
    {
        $message = 'Too many submissions from your network. Please wait before trying again.';
        if ($retryAfter instanceof \DateTimeInterface) {
            $message .= \sprintf(' You can retry after %s.', $retryAfter->format(DATE_ATOM));
        }

        parent::__construct($message);
    }
}
