<?php

declare(strict_types=1);

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');
    });
