<?php

use App\Http\Controllers\FlightController;
use App\Http\Controllers\FlightImportController;
use App\Http\Controllers\FlightProfileController;
use App\Http\Controllers\LogoController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\SearchController;
use App\Models\Airline;
use App\Models\FlightClass;
use App\Models\FlightReason;
use App\Models\SeatType;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;

/**
 * App Routes
 */
Route::domain(config('app.domain'))->group(
    function() {
        Route::get('/', function () {
            return Inertia::render(auth()->check() ? 'Dashboard' : 'Auth/Login');
        });



        Route::get('/dashboard', function () {
            return Inertia::render('Dashboard');
        })->middleware(['auth', 'verified'])->name('dashboard');

        Route::middleware('auth')->group(function () {
            Route::get('/import/fr24', function () {
                if (Auth::user()->importedFlights()->exists()) {
                    return redirect()->route('reconcile');
                }
                return Inertia::render('Fr24Import');
            })->name('import.fr24');

            Route::post('/flights', [FlightController::class, 'store'])->name('flights.store');
            Route::get('/flights/add', [FlightController::class, 'add'])->name('flights.add');
            Route::get('/flights/{flight}/edit', [FlightController::class, 'edit'])->name('flights.edit');
            Route::put('/flights/{flight}', [FlightController::class, 'update'])->name('flights.update');

            Route::get('/reconcile', function () {
                $flight = new FlightImportController()->reconcile(request());

                if (!$flight) {
                    return to_route('import.fr24');
                }

                return Inertia::render('ReconcileFlight', [
                    'flight' => $flight,
                    'key' => $flight['imported_flight_id'],
                ]);
            })->name('reconcile');

            Route::get('/flights/lookup', [FlightController::class, 'lookup'])->name('flights.lookup');
            Route::post('/flights/import', [FlightImportController::class, 'store'])->name('flights.import.store');

            Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
            Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
            Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
        });

        Route::post('/import/save', [FlightImportController::class, 'save'])->name('import.save');

        //Search Routes
        Route::get('/search/airlines', [SearchController::class, 'airlines'])->name('search.airlines');
        Route::get('/search/aircraft', [SearchController::class, 'aircraft'])->name('search.aircraft');
        Route::get('/search/airports', [SearchController::class, 'airports'])->name('search.airports');


        Route::get('/u/{username}', [FlightProfileController::class, 'view'])->name('profile.view');
        Route::get('/u/{username}/map', [FlightProfileController::class, 'map'])->name('profile.map');
        Route::get('/u/{username}/departure-board/{flight?}', [FlightProfileController::class, 'departureBoard'])
            ->name('profile.departure-board');
        Route::get('/u/{username}/boarding-passes', [FlightProfileController::class, 'boardingPasses'])->name('profile.boarding-passes');

        require __DIR__.'/auth.php';

    }
);


/**
 * API Routes
 */
Route::domain(config('app.api_domain'))->group(function () {
    Route::get('/', function () {
        return response()->json(['message' => 'Welcome to the FlightsGoneBy API']);
    });
    Route::get('airlines/logos/tail/{code}', [LogoController::class, 'getLogoByCode'])
        ->where('code', '[A-Za-z0-9]{2,3}');
    Route::get('airlines/logos/tail/id/{id}', [LogoController::class, 'getLogoById'])->where('id', '[0-9]+');
});
