<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        api: __DIR__.'/../routes/api.php',
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
        apiPrefix: 'api/v1',
    )
    ->withMiddleware(function (Middleware $middleware): void {
        $middleware->alias([
            'auth'       => \App\Http\Middleware\Authenticate::class,
            'store'      => \App\Http\Middleware\StoreIsolation::class,
            'permission' => \App\Http\Middleware\CheckPermission::class,
            'oplog'      => \App\Http\Middleware\OperationLog::class,
        ]);

        // Removed statefulApi() — app uses Bearer token auth, not cookie SPA auth
    })
    ->withExceptions(function (Exceptions $exceptions): void {
        $exceptions->render(function (\Illuminate\Auth\AuthenticationException $e, $request) {
            if ($request->expectsJson() || $request->is('api/*')) {
                return response()->json(['message' => '未认证，请先登录'], 401);
            }
        });
    })->create();
