<?php

declare(strict_types=1);

namespace App\Framework\ErrorHandling\Handlers;

use App\Framework\Exception\ValidationException;

/**
 * Handler for validation errors
 *
 * Priority: CRITICAL - Validates before any other processing
 */
final readonly class ValidationErrorHandler implements ErrorHandlerInterface
{
    public function canHandle(\Throwable $exception): bool
    {
        return $exception instanceof ValidationException;
    }

    public function handle(\Throwable $exception): HandlerResult
    {
        assert($exception instanceof ValidationException);

        return HandlerResult::create(
            handled: true,
            message: 'Validation failed',
            data: [
                'errors' => $exception->getErrors(),
                'validation_context' => $exception->context->data
            ],
            isFinal: true, // Stop handler chain for validation errors
            statusCode: 422
        );
    }

    public function getName(): string
    {
        return 'validation_error_handler';
    }

    public function getPriority(): ErrorHandlerPriority
    {
        return ErrorHandlerPriority::CRITICAL;
    }
}
