<?php

use App\Http\Middleware\AdminAuth;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Support\Facades\Route;

use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

return Application::configure(basePath: dirname(__DIR__))
	->withRouting(
		using: null,
		web: __DIR__ . '/../routes/web.php',
		api: null,
		commands: __DIR__ . '/../routes/console.php',
		channels: null,
		pages: null,
		health: '/up',
		apiPrefix: 'api',
		then: function () {
			Route::prefix('/admin')
				->middleware(['admin', 'guest:admin'])
				->name('admin.')
				->group(base_path('routes/admin.php'));
		},
	)
	->withMiddleware(function (Middleware $middleware) {
		$middleware->append([
			\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
			\Illuminate\Session\Middleware\StartSession::class,
			\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
			\Illuminate\View\Middleware\ShareErrorsFromSession::class,
		]);

		$middleware->appendToGroup('admin', [
			\Illuminate\Routing\Middleware\SubstituteBindings::class,
		]);

		$middleware->alias([
			'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
			'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
			'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
			'can' => \Illuminate\Auth\Middleware\Authorize::class,
			'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
			'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
			'auth:admin' => AdminAuth::class,
		]);

		$middleware->redirectGuestsTo(fn() => route('admin.login'));
	})
	->withExceptions(function (Exceptions $exceptions) {
		$exceptions->render(function (NotFoundHttpException $e, Request $request) {
			if ($request->is('api/*')) {
				return response()->json([
					'status' => 'false',
					'message' => 'Route Not found',
				], 404);
			}
		});
	})->create();
