<?php

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',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware): void {
        $middleware->alias([
            'api.client'      => \App\Http\Middleware\EnsureApiClientKey::class,
            'api.device'      => \App\Http\Middleware\EnsureDeviceApiKey::class,
            'check.installed' => \App\Http\Middleware\CheckInstallation::class,
        ]);
        $middleware->appendToGroup('web', \App\Http\Middleware\CheckInstallation::class);
    })
    ->withExceptions(function (Exceptions $exceptions): void {
        $exceptions->render(function (
            \Illuminate\Auth\AuthenticationException $e,
            \Illuminate\Http\Request $request,
        ) {
            if (! $request->expectsJson()) {
                return redirect()->route('admin.login');
            }
        });
    })->create();
