<?php

use App\Http\Middleware\AuthenticateService;
use App\Http\Middleware\EnsurePlatformSession;
use App\Http\Middleware\SetActingAccount;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Request;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
        then: function () {
            \Illuminate\Support\Facades\Route::bind('room', function (string $value) {
                return \App\Models\Room::where('uuid', $value)->firstOrFail();
            });
            \Illuminate\Support\Facades\Route::bind('session', function (string $value) {
                return \App\Models\Session::where('uuid', $value)->firstOrFail();
            });
            \Illuminate\Support\Facades\Route::bind('recording', function (string $value) {
                return \App\Models\Recording::where('uuid', $value)->firstOrFail();
            });
            \Illuminate\Support\Facades\Route::bind('summary', function (string $value) {
                return \App\Models\AiSummary::where('uuid', $value)->firstOrFail();
            });
            \Illuminate\Support\Facades\Route::bind('invitation', function (string $value) {
                return \App\Models\Invitation::where('uuid', $value)->firstOrFail();
            });
            \Illuminate\Support\Facades\Route::bind('webhook', function (string $value) {
                return \App\Models\WebhookEndpoint::where('uuid', $value)->firstOrFail();
            });
            \Illuminate\Support\Facades\Route::bind('poll', function (string $value) {
                return \App\Models\MeetPoll::where('uuid', $value)->firstOrFail();
            });
            \Illuminate\Support\Facades\Route::bind('breakout', function (string $value) {
                return \App\Models\BreakoutRoom::where('uuid', $value)->firstOrFail();
            });
            \Illuminate\Support\Facades\Route::bind('question', function (string $value) {
                return \App\Models\QaQuestion::where('uuid', $value)->firstOrFail();
            });
            \Illuminate\Support\Facades\Route::bind('file', function (string $value) {
                return \App\Models\SessionFile::where('uuid', $value)->firstOrFail();
            });
            \Illuminate\Support\Facades\Route::bind('registration', function (string $value) {
                return \App\Models\WebinarRegistration::where('uuid', $value)->firstOrFail();
            });
            \Illuminate\Support\Facades\Route::bind('participant', function (string $value) {
                return \App\Models\Participant::where('uuid', $value)->firstOrFail();
            });
        },
    )
    ->withMiddleware(function (Middleware $middleware): void {
        $middleware->redirectGuestsTo(function (Request $request) {
            $params = ['redirect' => $request->fullUrl()];

            if ($request->is('r/*', 'room/*')) {
                $params['interactive'] = 1;
            }

            return route('sso.connect', $params);
        });
        $middleware->web(append: [
            SetActingAccount::class,
        ]);
        $middleware->alias([
            'auth.service' => AuthenticateService::class,
            'platform.session' => EnsurePlatformSession::class,
            'meet.setup' => \App\Http\Middleware\EnsureOrganizationSetup::class,
            'meet.ability' => \App\Http\Middleware\EnsureMeetAbility::class,
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions): void {
        //
    })->create();
