<?php

use App\Http\Middleware\AuthenticateService;
use App\Http\Middleware\EnsurePlatformSession;
use App\Http\Middleware\SetActingAccount;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Request;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
        then: function () {
            \Illuminate\Support\Facades\Route::bind('investigation', function (string $value) {
                return \App\Models\InvestigationRequest::where('uuid', $value)->firstOrFail();
            });
        },
    )
    ->withMiddleware(function (Middleware $middleware): void {
        $middleware->validateCsrfTokens(except: [
            'display/*/announcements/*/played',
        ]);
        $middleware->redirectGuestsTo(fn (Request $request) => route('sso.connect', [
            'redirect' => $request->fullUrl(),
        ]));
        $middleware->web(append: [
            SetActingAccount::class,
        ]);
        $middleware->alias([
            'auth.service' => AuthenticateService::class,
            'platform.session' => EnsurePlatformSession::class,
            'care.setup' => \App\Http\Middleware\EnsureOrganizationSetup::class,
            'care.ability' => \App\Http\Middleware\EnsureCareAbility::class,
            'care.paid' => \App\Http\Middleware\EnsurePaidPlan::class,
            'care.device' => \App\Http\Middleware\AuthenticateCareDevice::class,
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions): void {
        //
    })->create();
