<?php

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

use App\Http\Helpers\PageUtility;
use Symfony\Component\HttpFoundation\Response;

return z_Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->alias([
            'auth' => \App\Http\Middleware\Authenticate::class,
            'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
            'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class,
            'grant.access' => \App\Http\Middleware\GrantAccess::class,
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
        $exceptions->respond(function (Response $response) {
            // if ($this->isHttpException($response) && $response->getStatusCode() === 404) {
            if ($response->getStatusCode() === 404) {
                $dataArr = PageUtility::data( ['class_id' => 8, 'page_id' => 2] );
                if (!empty($dataArr)) {
                    $dataArr['full_url'] = route('web.home');

                    $meta_title = $dataArr['meta_title'];
                    $dataArr['meta_title'] = $meta_title != '' ? strip_tags($meta_title) : strip_tags($dataArr['title']);

                    $appTitle = Config('app.name');
                    $defDataArr = array("def_img_ttl" => $appTitle, "def_img_alt" => $appTitle);

                    return response()->view('themes.frontend.pages.404', compact('defDataArr', 'dataArr'));
                }
            }    
            return $response;
        });
    })->create();
