<?php

use App\Http\Middleware\ApplyResolvedLocale;
use App\Http\Middleware\SuppressDebugbarForSmokeRequests;
use App\Http\Middleware\UseSystemSessionCookieForLivewireRequests;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware): void {
        $middleware->prepend(SuppressDebugbarForSmokeRequests::class);

        $middleware->encryptCookies(except: [
            SuppressDebugbarForSmokeRequests::COOKIE_NAME,
        ]);

        $middleware->web(prepend: [
            UseSystemSessionCookieForLivewireRequests::class,
        ]);

        $middleware->web(append: [
            ApplyResolvedLocale::class,
        ]);

        $middleware->alias([
            'apply-resolved-locale' => ApplyResolvedLocale::class,
            'ensure-correct-guard' => \App\Http\Middleware\EnsureCorrectGuard::class,
            'ensure-platform-capability' => \App\Http\Middleware\EnsurePlatformCapability::class,
            'ensure-workspace-member' => \App\Http\Middleware\EnsureWorkspaceMember::class,
            'ensure-workspace-selected' => \App\Http\Middleware\EnsureWorkspaceSelected::class,
            'ensure-environment-context-selected' => \App\Support\Middleware\EnsureEnvironmentContextSelected::class,
        ]);

        $middleware->prependToPriorityList(
            \Illuminate\Contracts\Auth\Middleware\AuthenticatesRequests::class,
            \App\Http\Middleware\EnsureCorrectGuard::class,
        );

        $middleware->redirectGuestsTo('/admin/login');
    })
    ->withExceptions(function (Exceptions $exceptions): void {
        //
    })->create();
