<?php

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

$app = Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware): void {
        $middleware->alias([
            'mobile.verified' => \App\Http\Middleware\CheckMobileVerified::class,
            'admin'           => \App\Http\Middleware\EnsureAdmin::class,
            'persian.digits'  => \App\Http\Middleware\NormalizePersianDigits::class,
            'torob.jwt'       => \App\Http\Middleware\VerifyTorobJwt::class,
        ]);

        $middleware->appendToGroup('web', [
            \App\Http\Middleware\ForceMaintenanceMode::class,
            \App\Http\Middleware\CaptureVisitorReferrer::class,
        ]);

        // TorobPay gateway posts back without CSRF token
        $middleware->validateCsrfTokens(except: [
            'checkout/callback/torobpay',
            'checkout/callback/digipay',
            'torob_api/v3/products',
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions): void {
        //
    })->create();

// Public directory is htdocs2 (one level above engine/)
$app->usePublicPath(dirname(dirname(__DIR__)));

return $app;
