<?php

use App\Http\Controllers\AdminController;
use App\Http\Controllers\FeedController;
use App\Http\Controllers\FlightController;
use App\Http\Controllers\FlightImportController;
use App\Http\Controllers\FollowerController;
use App\Http\Controllers\HomePageController;
use App\Http\Controllers\SupportFormController;
use App\Http\Controllers\UserProfileController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\SearchController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;

/**
* App Routes
*/
    Route::get('/', [UserProfileController::class, 'index'])->name('home');

    Route::get('/splash', [HomePageController::class, 'splash'])->name('splash');
    Route::get('/broadcasting/config', function () {
        return response()->json([
            'key' => config('broadcasting.connections.reverb.key'),
            'host' => config('broadcasting.connections.reverb.options.host'),
            'port' => config('broadcasting.connections.reverb.options.port'),
            'scheme' => config('broadcasting.connections.reverb.options.scheme'),
            'vapidPublicKey' => config('webpush.vapid.public_key'),
        ]);
    });


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

    Route::middleware(['auth', 'role:admin'])->prefix('admin')->name('admin.')->group(function () {
        Route::get('/', [AdminController::class, 'dashboard'])->name('dashboard');
        Route::get('/missing-liveries', [AdminController::class, 'reconcileMissingLiveries'])->name('reconcile-missing-liveries')->middleware('permission:reconcile_missing_liveries');
        Route::post('/ignore-missing-livery', [AdminController::class, 'ignoreMissingLivery'])->name('ignore-missing-livery');

        Route::get('/missing-airlines', [AdminController::class, 'reconcileMissingAirlines'])->name('reconcile-missing-airlines')->middleware('permission:reconcile_missing_liveries');
    });



Route::middleware(['auth', 'verified'])->group(function () {

        Route::post('/push-subscriptions', function (Illuminate\Http\Request $request) {
            $request->user()->updatePushSubscription(
                $request->endpoint,
                $request->keys['p256dh'] ?? null,
                $request->keys['auth'] ?? null
            );

            return response()->noContent();
        });

        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::delete('/flights/{flight}/{referrer?}', [FlightController::class, 'delete'])->name('flights.delete');

        Route::get('/flights/lookup', [FlightController::class, 'lookup'])->name('flights.lookup');
        Route::post('/flights/import', [FlightImportController::class, 'store'])->name('flights.import.store');
        Route::get('/flights/{flight}/copy', [UserProfileController::class, 'copyFlight'])->name('flights.copy');

        Route::patch('/settings/{key}', [SettingsController::class, 'updateSingle'])
            ->where('key', '[a-z_]+')
            ->name('settings.update-single');


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

        Route::get('/reconcile', [FlightImportController::class, 'reconcile'])->name('reconcile');;

        Route::get('/settings/{category?}', [UserController::class, 'settings'])->name('user.settings');


        Route::get('/notifications', [NotificationController::class, 'index'])->name('notifications.get');
        Route::patch('/notifications/{notification}/read', [NotificationController::class, 'markRead']);

        Route::get('/feed', [FeedController::class, 'view'])->name('feed');

        Route::patch('/settings', [SettingsController::class, 'update'])->name('settings.update');

        Route::post('/u/{user}/follow', [FollowerController::class, 'follow'])->name('profile.follow');
        Route::get('/followers', [FollowerController::class, 'index'])->name('followers.index');
        Route::post('/followers/{follower}/approve', [FollowerController::class, 'approve'])->name('followers.approve');
        Route::post('/followers/{follower}/deny', [FollowerController::class, 'deny'])->name('followers.deny');
        Route::delete('/followers/{follower}', [FollowerController::class, 'remove'])->name('followers.remove');

    });

Route::middleware(['auth'])->group(function () {
        Route::get('/support', [SupportFormController::class, 'view'])->name('support');
        Route::post('/support', [SupportFormController::class, 'store'])->name('support.store');
    });



//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/{user}', [UserProfileController::class, 'view'])->name('profile.view');
    Route::get('/u/{user}/map', [UserProfileController::class, 'map'])->name('profile.map');
    Route::get('/u/{user}/departure-board/{flight?}', [UserProfileController::class, 'departureBoard'])->name('profile.departure-board');
    Route::get('/u/{user}/boarding-passes', [UserProfileController::class, 'boardingPasses'])->name('profile.boarding-passes');
    Route::get('/u/{user}/achievements', [UserProfileController::class, 'achievements'])->name('profile.achievements');
    Route::get('/u/{user}/achievement/{achievement}', [UserProfileController::class, 'achievement'])->name('profile.achievement');
    Route::get('/u/{user}/flight/{userFlight}', [UserProfileController::class, 'flight'])->name('profile.flight');


    require __DIR__.'/auth.php';

