<?php

declare(strict_types=1);

namespace Tests\Unit\Framework\ErrorHandling\Handlers;

use App\Framework\ErrorHandling\Handlers\ErrorHandlerPriority;
use App\Framework\ErrorHandling\Handlers\ValidationErrorHandler;
use App\Framework\Validation\Exceptions\ValidationException;

describe('ValidationErrorHandler', function () {
    beforeEach(function () {
        $this->handler = new ValidationErrorHandler();
    });

    it('handles ValidationException', function () {
        $validationResult = new \App\Framework\Validation\ValidationResult();
        $validationResult->addErrors('email', ['Email is required', 'Email format is invalid']);
        $validationResult->addErrors('password', ['Password must be at least 8 characters']);

        $exception = new ValidationException($validationResult);

        expect($this->handler->canHandle($exception))->toBeTrue();

        $result = $this->handler->handle($exception);

        expect($result->handled)->toBeTrue();
        expect($result->statusCode)->toBe(422);
        expect($result->data)->toHaveKey('errors');
        expect($result->data['errors'])->toBe($validationResult->getAll());
        expect($result->data['error_type'])->toBe('validation');
    });

    it('does not handle non-ValidationException', function () {
        $exception = new \RuntimeException('Some error');

        expect($this->handler->canHandle($exception))->toBeFalse();
    });

    it('has CRITICAL priority', function () {
        expect($this->handler->getPriority())->toBe(ErrorHandlerPriority::CRITICAL);
    });

    it('has correct name', function () {
        expect($this->handler->getName())->toBe('validation_error_handler');
    });
});
