<?php

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

// Suppress PHP 8.5 deprecation warnings for PDO constants in vendor files
// This is a temporary fix until Laravel framework is updated
if (PHP_VERSION_ID >= 80500) {
    set_error_handler(function ($errno, $errstr, $errfile, $errline) {
        if ($errno === E_DEPRECATED &&
            str_contains($errfile, 'vendor/laravel/framework/config/database.php') &&
            str_contains($errstr, 'PDO::MYSQL_ATTR_SSL_CA')) {
            return true; // Suppress this specific warning
        }

        return false; // Let other errors through
    }, E_DEPRECATED);
}

return 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([
            'permission' => \App\Http\Middleware\CheckPermission::class,
            'role' => \App\Http\Middleware\CheckRole::class,
            'installer.check' => \App\Http\Middleware\CheckInstalled::class,
            'installer.verify' => \App\Http\Middleware\VerifyInstallerIntegrity::class,
            'lix' => \App\Http\Middleware\LixMiddleware::class,
            'customer.active' => \App\Http\Middleware\EnsureCustomerIsActive::class,
            'rate.limit' => \App\Http\Middleware\RateLimitMiddleware::class,
        ]);

        // Set locale based on user settings
        $middleware->web(prepend: [
            \App\Http\Middleware\SetLocale::class,
        ]);

        // Add security headers to all web requests
        $middleware->web(append: [
            \App\Http\Middleware\SecurityHeaders::class,
        ]);

        // Verify installer integrity on all routes (except installer routes)
        $middleware->web(append: [
            \App\Http\Middleware\VerifyInstallerIntegrity::class,
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions): void {
        //
    })->create();
