<?php

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

/*
|--------------------------------------------------------------------------
| Ensure Laravel runtime directories exist
|--------------------------------------------------------------------------
|
| Some shared-hosting ZIP extractors omit empty directories. Laravel needs
| these paths before it can write sessions, compiled views, cache and logs.
| Creating them here prevents "Failed to open stream: No such file or
| directory" immediately after deployment.
|
*/
$runtimeDirectories = [
    dirname(__DIR__) . '/storage/app/public',
    dirname(__DIR__) . '/storage/framework/cache/data',
    dirname(__DIR__) . '/storage/framework/sessions',
    dirname(__DIR__) . '/storage/framework/testing',
    dirname(__DIR__) . '/storage/framework/views',
    dirname(__DIR__) . '/storage/logs',
    dirname(__DIR__) . '/bootstrap/cache',
];

foreach ($runtimeDirectories as $directory) {
    if (! is_dir($directory)) {
        @mkdir($directory, 0775, true);
    }
}

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->append(\App\Http\Middleware\SecurityHeaders::class);
        $middleware->alias([
            'admin' => \App\Http\Middleware\AdminAuthenticate::class,
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions): void {
        //
    })
    ->create();
