<?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-placeholder.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
        then: function () {
            // Module public routes (e.g. Pilkades' {kabupaten}/{kecamatan}/{desa}
            // wildcard) MUST be registered after web.php/auth.php so system
            // routes always win the match — otherwise a same-shape wildcard
            // route silently swallows requests like verify-email/{id}/{hash}.
            (new App\Providers\ModuleServiceProvider(app()))->registerModuleRoutes();
        },
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->alias([
            'super_admin' => \App\Modules\Pilkades\Http\Middleware\EnsureSuperAdmin::class,
            'admin_desa' => \App\Modules\Pilkades\Http\Middleware\EnsureAdminDesa::class,
            'tenant_access' => \App\Modules\Pilkades\Http\Middleware\EnsureTenantAccess::class,
            'throttle_voter_search' => \App\Modules\Pilkades\Http\Middleware\ThrottleVoterSearch::class,
            'ability' => \App\Modules\Pilkades\Http\Middleware\EnsureApiAbility::class,
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();
