<?php

use App\Http\Controllers\Client\BookingController;
use App\Http\Controllers\Client\HomeController;
use App\Http\Controllers\Client\RoomController;
use App\Models\CurrencyRate;
use App\Services\Client\BookingSessionService;
use App\Services\Client\CurrencyService;
use App\Services\Client\SearchSessionService;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Session;

use Illuminate\Http\Request;

Route::middleware(['web', 'client'])->group(function () {
    Route::get('/', [HomeController::class, 'index'])->name('home');

    Route::post('/search-rooms', [RoomController::class, 'search']);
    Route::get('/rooms', [RoomController::class, 'rooms'])->name('rooms');
    Route::get('/rooms/{slug}', [RoomController::class, 'show'])->name('rooms.show');
    Route::get('/all-rooms/{slug}', [RoomController::class, 'showAll'])->name('rooms.showAll');

    Route::get('/summary', [BookingController::class, 'summary'])->name('summary');
    Route::get('/checkout', [BookingController::class, 'checkout'])->name('checkout');

    Route::get('/booking/success', [BookingController::class, 'success'])->name('booking.success');
    Route::get('/booking/view', [BookingController::class, 'view'])->name('booking.view');
});

Route::post('/booking/add-room', [BookingController::class, 'addRoom'])->name('booking.addRoom');
Route::post('/booking/remove-room', [BookingController::class, 'removeRoom'])->name('booking.removeRoom');
Route::post('/booking/save-summary', [BookingController::class, 'saveSummary'])->name('booking.save-summary');
Route::post('/booking/save-summary/{slug}/{room_number}', [BookingController::class, 'saveSummeryAndRoomDetails'])->name('booking.save-summary-add-room-details');
Route::post('/booking/update-checkout', [BookingController::class, 'updateCheckout'])->name('booking.updateCheckout');
Route::post('/booking/store', [BookingController::class, 'store'])->name('booking.store');
Route::get('/booking/clear', [BookingController::class, 'clear'])->name('booking.clear');
Route::get('/booking/test', [BookingController::class, 'test'])->name('booking.test');


// Update teh currency when select by guest
Route::post('/currency/change', function (Request $request) {
    $request->validate(['currency' => 'required|string']);

    // Initialize session if missing
    CurrencyService::init();

    // Update selected currency
    CurrencyService::update(['selected' => $request->currency]);

    return back();
})->name('currency.change');


// Clear the session, only used for debug
Route::get('/clear', function (Request $request) {

    SearchSessionService::clear();
    CurrencyService::clear();
    BookingSessionService::clear();

    return redirect('/');
});
