<?php

use App\Http\Middleware\CreditCheckMiddleware;
use App\Http\Middleware\EnsureTenantAdminToken;
use App\Http\Middleware\EnsureTenantCollaboratorToken;
use App\Http\Middleware\HandleAppearance;
use App\Http\Middleware\HandleInertiaRequests;
use App\Http\Middleware\SetLocaleFromUser;
use App\Http\Middleware\TenantIsolation;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets;
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',
    )
    ->withCommands([
        \App\Console\Commands\CheckEventPackages::class,
        \App\Console\Commands\ExportCouponRedemptions::class,
        \App\Console\Commands\DeactivateExpiredPhotoboothAccounts::class,
        \App\Console\Commands\PhotoboothIngestCommand::class,
        \App\Console\Commands\MonitorStorageCommand::class,
        \App\Console\Commands\DispatchStorageArchiveCommand::class,
        \App\Console\Commands\CheckUploadQueuesCommand::class,
        \App\Console\Commands\PurgeExpiredDataExports::class,
        \App\Console\Commands\ProcessTenantRetention::class,
        \App\Console\Commands\SendGuestFeedbackReminders::class,
        \App\Console\Commands\SyncGoogleFonts::class,
        \App\Console\Commands\SeedDemoSwitcherTenants::class,
    ])
    ->withSchedule(function (\Illuminate\Console\Scheduling\Schedule $schedule) {
        $schedule->command('package:check-status')->dailyAt('06:00');
        $schedule->command('photobooth:cleanup-expired')->hourly()->withoutOverlapping();
        $schedule->command('photobooth:ingest')->everyFiveMinutes()->withoutOverlapping();
        $schedule->command('exports:purge')->dailyAt('02:00');
        $schedule->command('tenants:retention-scan')->dailyAt('03:00');
        $schedule->command('guest:feedback-reminders')->dailyAt('22:00');
    })
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->trustProxies(
            at: array_map('trim', explode(',', (string) env('TRUSTED_PROXIES', '*'))),
            headers: Request::HEADER_X_FORWARDED_FOR
                | Request::HEADER_X_FORWARDED_HOST
                | Request::HEADER_X_FORWARDED_PORT
                | Request::HEADER_X_FORWARDED_PROTO
                | Request::HEADER_X_FORWARDED_AWS_ELB,
        );

        $middleware->alias([
            'tenant.isolation' => TenantIsolation::class,
            'package.check' => \App\Http\Middleware\PackageMiddleware::class,
            'locale' => \App\Http\Middleware\SetLocale::class,
            'superadmin.auth' => \App\Http\Middleware\SuperAdminAuth::class,
            'credit.check' => CreditCheckMiddleware::class,
            'tenant.admin' => EnsureTenantAdminToken::class,
            'tenant.collaborator' => EnsureTenantCollaboratorToken::class,
        ]);

        $middleware->encryptCookies(except: ['appearance', 'sidebar_state']);

        $middleware->web(append: [
            \App\Http\Middleware\SetLocale::class,
            SetLocaleFromUser::class,
            HandleAppearance::class,
            \App\Http\Middleware\ContentSecurityPolicy::class,
            HandleInertiaRequests::class,
            AddLinkHeadersForPreloadedAssets::class,
            \App\Http\Middleware\RequestTimingMiddleware::class,
        ]);

        $middleware->api(append: []);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();
