<?php

declare(strict_types=1);

namespace App\Framework\ErrorHandling;

use App\Framework\DI\Container;
use App\Framework\DI\Initializer;
use App\Framework\ErrorHandling\Handlers\DatabaseErrorHandler;
use App\Framework\ErrorHandling\Handlers\FallbackErrorHandler;
use App\Framework\ErrorHandling\Handlers\HttpErrorHandler;
use App\Framework\ErrorHandling\Handlers\ValidationErrorHandler;
use App\Framework\Logging\Logger;

/**
 * Initializes the error handler system
 *
 * Registers all error handlers in priority order
 */
final readonly class ErrorHandlerInitializer
{
    #[Initializer]
    public function initialize(Container $container): ErrorHandlerManager
    {
        $logger = $container->get(Logger::class);
        $registry = new ErrorHandlerRegistry();
        $manager = new ErrorHandlerManager($registry);

        // Register all handlers at once (will be sorted by priority internally)
        return $manager->register(
            new ValidationErrorHandler(),       // CRITICAL priority
            new DatabaseErrorHandler($logger),  // HIGH priority
            new HttpErrorHandler(),             // NORMAL priority
            new FallbackErrorHandler($logger)   // LOWEST priority
        );
    }
}
