<?php

use App\Http\Controllers\Admin\BookingController as AdminBookingController;
use App\Http\Controllers\Admin\BookingBlockController;
use App\Http\Controllers\Admin\DashboardController as AdminDashboardController;
use App\Http\Controllers\Admin\GuestController as AdminGuestController;
use App\Http\Controllers\Admin\RateImportController;
use App\Http\Controllers\Admin\PoliceRegistrationController;
use App\Http\Controllers\Auth\AdminAuthController;
use App\Http\Controllers\Guest\AuthController as GuestAuthController;
use App\Http\Controllers\Guest\BookingController as GuestBookingController;
use App\Http\Controllers\Guest\DashboardController as GuestDashboardController;
use App\Http\Controllers\Admin\GuestAccessController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\PublicPageController;
use Illuminate\Support\Facades\Route;

Route::get('/', HomeController::class)->name('home');
Route::get('/contacts', [PublicPageController::class, 'contacts'])->name('public.contacts');
Route::get('/where-to-go', [PublicPageController::class, 'where'])->name('public.where');
Route::get('/rooms', [PublicPageController::class, 'rooms'])->name('public.rooms');
Route::get('/rooms/{room}', [PublicPageController::class, 'room'])->name('public.rooms.show');
Route::get('/booking', [PublicPageController::class, 'bookingOffer'])->name('public.booking_offer');

Route::get('/guest/bookings/create', fn () => redirect()->route('public.booking_offer'))->name('guest.bookings.create');
Route::get('/guest/bookings/quote', [GuestBookingController::class, 'quote'])->middleware('throttle:booking-read')->name('guest.bookings.quote');
Route::get('/guest/bookings/availability', [GuestBookingController::class, 'availability'])->middleware('throttle:booking-read')->name('guest.bookings.availability');
Route::post('/guest/bookings', [GuestBookingController::class, 'store'])->middleware('throttle:booking-submit')->name('guest.bookings.store');
Route::get('/guest/bookings/created/{bookingNumber}', [GuestBookingController::class, 'created'])->name('guest.bookings.created');
Route::post('/guest/bookings/receipt', [GuestBookingController::class, 'uploadReceiptPublic'])->middleware('throttle:booking-upload')->name('guest.bookings.receipt.public');
Route::post('/guest/bookings/documents', [GuestBookingController::class, 'uploadGuestDocumentsPublic'])->middleware('throttle:booking-upload')->name('guest.bookings.documents.public');

Route::prefix('guest')->name('guest.')->group(function (): void {
    Route::middleware(['guest.only', 'guest.access'])->group(function (): void {
        Route::get('/login', [GuestAuthController::class, 'showLogin'])->name('login');
        Route::post('/login', [GuestAuthController::class, 'login'])->middleware('throttle:guest-login')->name('login.store');
        Route::get('/register', [GuestAuthController::class, 'showRegister'])->name('register');
        Route::post('/register', [GuestAuthController::class, 'register'])->middleware('throttle:guest-register')->name('register.store');
    });

    Route::middleware(['guest.auth', 'guest.access'])->group(function (): void {
        Route::get('/', GuestDashboardController::class)->name('dashboard');
        Route::get('/dashboard', GuestDashboardController::class)->name('dashboard.alias');
        Route::get('/bookings', [GuestBookingController::class, 'index'])->name('bookings.index');
        Route::get('/bookings/create-discount', [GuestBookingController::class, 'createDiscount'])->name('bookings.create_discount');
        Route::get('/bookings/{booking}', [GuestBookingController::class, 'show'])->name('bookings.show');
        Route::post('/bookings/{booking}/receipt', [GuestBookingController::class, 'uploadReceipt'])->middleware('throttle:booking-upload')->name('bookings.receipt.store');
        Route::get('/bookings/{booking}/documents/{document}', [GuestBookingController::class, 'downloadDocument'])->name('bookings.documents.download');
        Route::post('/logout', [GuestAuthController::class, 'logout'])->name('logout');
    });
});

Route::prefix('admin')->name('admin.')->group(function (): void {
    Route::middleware('guest.admin')->group(function (): void {
        Route::get('/login', [AdminAuthController::class, 'create'])->name('login');
        Route::post('/login', [AdminAuthController::class, 'store'])->middleware('throttle:admin-login')->name('login.store');
    });

    Route::middleware('admin')->group(function (): void {
        Route::get('/', [AdminDashboardController::class, 'index'])->name('dashboard');
        Route::get('/dashboard', [AdminDashboardController::class, 'index'])->name('dashboard.alias');
        Route::post('/deadline-rules', [AdminDashboardController::class, 'storeDeadlineRule'])->name('deadline_rules.store');
        Route::delete('/deadline-rules/{deadlineRule}', [AdminDashboardController::class, 'destroyDeadlineRule'])->name('deadline_rules.destroy');
        Route::get('/bookings', [AdminBookingController::class, 'index'])->name('bookings.index');
        Route::get('/guests', [AdminGuestController::class, 'index'])->name('guests.index');
        Route::delete('/guests/cleanup/by-phone', [AdminGuestController::class, 'cleanupByPhone'])->name('guests.cleanup.by_phone');
        Route::delete('/guests/{guest}', [AdminGuestController::class, 'destroy'])->name('guests.destroy');
        Route::get('/police-registrations', [PoliceRegistrationController::class, 'index'])->name('police_registrations.index');
        Route::get('/police-registrations/download', [PoliceRegistrationController::class, 'download'])->name('police_registrations.download');
        Route::get('/police-registrations/daily-arrivals', [PoliceRegistrationController::class, 'downloadDailyArrivals'])->name('police_registrations.daily_arrivals.download');
        Route::delete('/police-registrations/{registration}', [PoliceRegistrationController::class, 'destroy'])->name('police_registrations.destroy');
        Route::get('/police-registrations/{registration}/files/{slot}', [PoliceRegistrationController::class, 'downloadFile'])
            ->where('slot', 'front|back|txt')
            ->name('police_registrations.files.download');
        Route::get('/booking-blocks', [BookingBlockController::class, 'index'])->name('booking_blocks.index');
        Route::post('/booking-blocks/google-ping', [BookingBlockController::class, 'ping'])->name('booking_blocks.ping');
        Route::post('/booking-blocks/google-logs/clear', [BookingBlockController::class, 'clearLogs'])->name('booking_blocks.logs.clear');
        Route::post('/booking-blocks', [BookingBlockController::class, 'store'])->name('booking_blocks.store');
        Route::delete('/booking-blocks', [BookingBlockController::class, 'destroy'])->name('booking_blocks.destroy');
        Route::get('/bookings/create', [AdminBookingController::class, 'create'])->name('bookings.create');
        Route::get('/bookings/quote', [AdminBookingController::class, 'quote'])->middleware('throttle:booking-read')->name('bookings.quote');
        Route::get('/bookings/availability', [AdminBookingController::class, 'availability'])->middleware('throttle:booking-read')->name('bookings.availability');
        Route::post('/bookings', [AdminBookingController::class, 'store'])->middleware('throttle:booking-submit')->name('bookings.store');
        Route::get('/bookings/{booking}/created', [AdminBookingController::class, 'created'])->name('bookings.created');
        Route::get('/bookings/{booking}', [AdminBookingController::class, 'show'])->name('bookings.show');
        Route::post('/bookings/{booking}/cancel', [AdminBookingController::class, 'cancel'])->name('bookings.cancel');
        Route::post('/bookings/{booking}/payments', [AdminBookingController::class, 'storePayment'])->name('bookings.payments.store');
        Route::post('/bookings/{booking}/payments/reject', [AdminBookingController::class, 'rejectPayment'])->name('bookings.payments.reject');
        Route::post('/bookings/{booking}/sync/retry', [AdminBookingController::class, 'retrySync'])->name('bookings.sync.retry');
        Route::post('/bookings/{booking}/flags', [AdminBookingController::class, 'updateFlags'])->name('bookings.flags.update');
        Route::post('/bookings/{booking}/guest-rating', [AdminBookingController::class, 'updateGuestRating'])->name('bookings.guest_rating.update');
        Route::delete('/bookings/{booking}', [AdminBookingController::class, 'destroy'])->name('bookings.destroy');
        Route::post('/bookings/{booking}/documents', [AdminBookingController::class, 'uploadDocument'])->middleware('throttle:booking-upload')->name('bookings.documents.upload');
        Route::get('/bookings/{booking}/folder-files/{filename}/view', [AdminBookingController::class, 'viewFolderFile'])->where('filename', '[^/]+')->name('bookings.folder_files.view');
        Route::get('/bookings/{booking}/folder-files/{filename}', [AdminBookingController::class, 'downloadFolderFile'])->where('filename', '[^/]+')->name('bookings.folder_files.download');
        Route::get('/bookings/{booking}/documents/{document}/view', [AdminBookingController::class, 'viewDocument'])->name('bookings.documents.view');
        Route::get('/bookings/{booking}/documents/{document}', [AdminBookingController::class, 'downloadDocument'])->name('bookings.documents.download');

        Route::get('/rate-imports', [RateImportController::class, 'index'])->name('rate_imports.index');
        Route::post('/rate-imports/run', [RateImportController::class, 'run'])->name('rate_imports.run');

        Route::post('/guest-access/toggle', [GuestAccessController::class, 'toggle'])->name('guest_access.toggle');
        Route::post('/logout', [AdminAuthController::class, 'destroy'])->name('logout');
    });
});
