<?php

declare(strict_types=1);

namespace App\Services\SupportCenter;

use Illuminate\Contracts\Foundation\Application;

/**
 * 按配置装配系统更新维护模式 / 缓存清理命令执行器。
 */
final class HostSystemUpdateHandlerFactory
{
    public function __construct(
        private readonly Application $app,
    ) {}

    public function maintenanceHandler(): HostSystemUpdateMaintenanceCommandHandler
    {
        $mode = HostSystemUpdateHandlerMode::normalize(
            config('dberp.system_update.maintenance_handler'),
        );

        $inProcess = $this->app->make(InProcessHostSystemUpdateMaintenanceCommandHandler::class);
        $process = $this->app->make(ProcessHostSystemUpdateMaintenanceCommandHandler::class);

        return match ($mode) {
            HostSystemUpdateHandlerMode::PROCESS => $process,
            HostSystemUpdateHandlerMode::FALLBACK => new FallbackHostSystemUpdateMaintenanceCommandHandler(
                $inProcess,
                $process,
            ),
            default => $inProcess,
        };
    }

    public function artisanCommandHandler(): HostSystemUpdateArtisanCommandHandler
    {
        $mode = HostSystemUpdateHandlerMode::normalize(
            config('dberp.system_update.cache_clear_handler'),
        );

        $inProcess = $this->app->make(InProcessHostSystemUpdateArtisanCommandHandler::class);
        $process = $this->app->make(ProcessHostSystemUpdateArtisanCommandHandler::class);

        return match ($mode) {
            HostSystemUpdateHandlerMode::PROCESS => $process,
            HostSystemUpdateHandlerMode::FALLBACK => new FallbackHostSystemUpdateArtisanCommandHandler(
                $inProcess,
                $process,
            ),
            default => $inProcess,
        };
    }
}
