<?php

declare(strict_types=1);

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

use App\Http\Controllers\Install\InstallDatabaseController;
use App\Http\Controllers\Install\InstallEnvironmentController;
use App\Http\Controllers\Install\InstallFinishedController;
use App\Http\Controllers\Install\InstallInitializeController;
use App\Http\Controllers\Install\InstallProgressController;
use App\Http\Controllers\Install\InstallWelcomeController;
use Illuminate\Support\Facades\Route;

Route::prefix('install')
    ->name('install.')
    ->group(function (): void {
        Route::middleware(['dberp.not_installed'])->group(function (): void {
            Route::get('/', [InstallWelcomeController::class, 'show'])->name('welcome');
            Route::get('/environment', [InstallEnvironmentController::class, 'show'])->name('environment');
            Route::get('/database', [InstallDatabaseController::class, 'show'])->name('database');
            Route::post('/database', [InstallDatabaseController::class, 'store'])->name('database.store');
            Route::get('/initialize', [InstallInitializeController::class, 'show'])->name('initialize');
            Route::post('/initialize', [InstallInitializeController::class, 'store'])->name('initialize.store');
        });

        /*
         * 进度页必须在「已写入安装标记」之后仍可访问：此时 isInstalled() 已为 true，
         * 若仍套 dberp.not_installed，后续 run-step 会被重定向，收尾与完成阶段无法执行。
         */
        $installIdPattern = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
        Route::get('/progress/{install_id}', [InstallProgressController::class, 'show'])
            ->where('install_id', $installIdPattern)
            ->name('progress');
        Route::get('/progress/{install_id}/state', [InstallProgressController::class, 'state'])
            ->where('install_id', $installIdPattern)
            ->name('progress.state');
        Route::post('/progress/{install_id}/run-step', [InstallProgressController::class, 'runStep'])
            ->where('install_id', $installIdPattern)
            ->name('progress.run-step');
        Route::post('/progress/{install_id}/retry-step', [InstallProgressController::class, 'retryStep'])
            ->where('install_id', $installIdPattern)
            ->name('progress.retry-step');

        Route::get('/finished', [InstallFinishedController::class, 'show'])
            ->middleware(['dberp.installed'])
            ->name('finished');
    });
