<?php

use App\Http\Middleware\EnsureBranchAccess;
use App\Http\Middleware\EnsureModuleEnabled;
use App\Http\Middleware\EnsurePermission;
use App\Http\Middleware\NormalizePhoneInputs;
use App\Http\Middleware\OptimizeUploadedImages;
use App\Http\Middleware\SecurityHeaders;
use App\Http\Middleware\SetLocale;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Support\Facades\Route;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
        then: function (): void {
            Route::middleware('web')->group(base_path('routes/localization.php'));
            Route::middleware('web')->group(base_path('routes/daily-reports.php'));
            Route::middleware('web')->group(base_path('routes/system-backups.php'));
            Route::middleware('web')->group(base_path('routes/laravel-logs.php'));
            Route::middleware('web')->group(base_path('routes/system-reset.php'));
        },
    )
    ->withMiddleware(function (Middleware $middleware): void {
        $middleware->redirectGuestsTo('/login');
        $middleware->redirectUsersTo('/dashboard');
        $middleware->web(append: [
            OptimizeUploadedImages::class,
            NormalizePhoneInputs::class,
            SetLocale::class,
            EnsureBranchAccess::class,
            SecurityHeaders::class,
        ]);
        $middleware->alias([
            'permission' => EnsurePermission::class,
            'branch.access' => EnsureBranchAccess::class,
            'module' => EnsureModuleEnabled::class,
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions): void {
        //
    })->create();
