<?php

use App\Http\Middleware\BlockReadOnlyImpersonationWrites;
use App\Http\Middleware\EnsureAccountNotBlocked;
use App\Http\Middleware\EnsureEmailIsVerifiedUnlessAdmin;
use App\Http\Middleware\EnsureMaintenanceModeAccess;
use App\Http\Middleware\EnsureInvestorKycApproved;
use App\Http\Middleware\EnsureUserIsAdmin;
use App\Http\Middleware\HandleAppearance;
use App\Http\Middleware\HandleInertiaRequests;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets;

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->encryptCookies(except: ['appearance', 'sidebar_state']);

        $middleware->alias([
            'admin' => EnsureUserIsAdmin::class,
            'investor.kyc' => EnsureInvestorKycApproved::class,
            'verified' => EnsureEmailIsVerifiedUnlessAdmin::class,
        ]);

        $middleware->web(append: [
            EnsureMaintenanceModeAccess::class,
            HandleAppearance::class,
            HandleInertiaRequests::class,
            AddLinkHeadersForPreloadedAssets::class,
            EnsureAccountNotBlocked::class,
            BlockReadOnlyImpersonationWrites::class,
        ]);

        $middleware->prependToPriorityList(
            \Illuminate\Contracts\Auth\Middleware\AuthenticatesRequests::class,
            EnsureMaintenanceModeAccess::class,
        );
    })
    ->withExceptions(function (Exceptions $exceptions): void {
        //
    })->create();
