<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

namespace App\Http\Controllers\Install;

use App\Http\Controllers\Controller;
use App\Services\Install\Contracts\WebInstallStepRunnerContract;
use App\Services\Install\DBErpInstallStatus;
use App\Services\Install\WebInstallProgressApiResponse;
use App\Services\Install\WebInstallProgressDisplay;
use App\Services\Install\WebInstallProgressNotFoundException;
use App\Services\Install\WebInstallProgressSessionManager;
use Illuminate\Contracts\View\View;
use Illuminate\Http\JsonResponse;
use InvalidArgumentException;

final class InstallProgressController extends Controller
{
    public function show(
        string $installId,
        WebInstallProgressSessionManager $progressSessionManager,
    ): View {
        try {
            $progress = $progressSessionManager->read($installId);
        } catch (InvalidArgumentException) {
            $progress = null;
        }

        if ($progress === null) {
            return view('install.progress', [
                'missingTask' => true,
                'missingTaskMessage' => WebInstallProgressDisplay::MISSING_TASK_MESSAGE,
            ]);
        }

        return view('install.progress', [
            'missingTask' => false,
            'installId' => $installId,
            'progress' => $progress,
            'headline' => WebInstallProgressDisplay::INSTALL_HEADLINE,
            'stages' => WebInstallProgressDisplay::businessStages(),
            'stateUrl' => route('install.progress.state', ['install_id' => $installId]),
            'runStepUrl' => route('install.progress.run-step', ['install_id' => $installId]),
            'retryStepUrl' => route('install.progress.retry-step', ['install_id' => $installId]),
            'finishedUrl' => route('install.finished'),
        ]);
    }

    public function state(
        string $installId,
        WebInstallProgressSessionManager $progressSessionManager,
        DBErpInstallStatus $installStatus,
    ): JsonResponse {
        try {
            $progress = $progressSessionManager->read($installId);
        } catch (InvalidArgumentException) {
            $progress = null;
        }

        if ($progress === null) {
            return response()->json([
                'message' => WebInstallProgressDisplay::MISSING_TASK_MESSAGE,
            ], 404);
        }

        return response()->json(WebInstallProgressApiResponse::fromState(
            $progress,
            applicationAlreadyInstalled: $installStatus->isInstalled(),
        ));
    }

    public function runStep(
        string $installId,
        WebInstallStepRunnerContract $stepRunner,
        DBErpInstallStatus $installStatus,
    ): JsonResponse {
        try {
            $progress = $stepRunner->runNextStep($installId);
        } catch (WebInstallProgressNotFoundException) {
            return response()->json([
                'message' => WebInstallProgressDisplay::MISSING_TASK_MESSAGE,
            ], 404);
        } catch (InvalidArgumentException) {
            return response()->json([
                'message' => WebInstallProgressDisplay::MISSING_TASK_MESSAGE,
            ], 404);
        }

        return response()->json(WebInstallProgressApiResponse::fromState(
            $progress,
            applicationAlreadyInstalled: $installStatus->isInstalled(),
        ));
    }

    public function retryStep(
        string $installId,
        WebInstallStepRunnerContract $stepRunner,
        DBErpInstallStatus $installStatus,
    ): JsonResponse {
        try {
            $progress = $stepRunner->retryFailedStep($installId);
        } catch (WebInstallProgressNotFoundException) {
            return response()->json([
                'message' => WebInstallProgressDisplay::MISSING_TASK_MESSAGE,
            ], 404);
        } catch (InvalidArgumentException) {
            return response()->json([
                'message' => WebInstallProgressDisplay::MISSING_TASK_MESSAGE,
            ], 404);
        }

        return response()->json(WebInstallProgressApiResponse::fromState(
            $progress,
            applicationAlreadyInstalled: $installStatus->isInstalled(),
        ));
    }
}
