<?php

use App\Http\Controllers\VoyagedifiantDashboard\ChauffeurController;
use App\Http\Controllers\VoyagedifiantDashboard\ForgotPasswordController;
use App\Http\Controllers\VoyagedifiantDashboard\formConducteurController;
use App\Http\Controllers\VoyagedifiantDashboard\formeSiteController;
use App\Http\Controllers\VoyagedifiantDashboard\formHotelController;
use App\Http\Controllers\VoyagedifiantDashboard\formUtilisateurController;
use App\Http\Controllers\VoyagedifiantDashboard\formVehiculeController;
use App\Http\Controllers\VoyagedifiantDashboard\HotelController;
use App\Http\Controllers\VoyagedifiantDashboard\IndexController;
use App\Http\Controllers\VoyagedifiantDashboard\listeConducteurController;
use App\Http\Controllers\VoyagedifiantDashboard\listedeSiteController;
use App\Http\Controllers\VoyagedifiantDashboard\listeHotelController;
use App\Http\Controllers\VoyagedifiantDashboard\listeUtilisateurController;
use App\Http\Controllers\VoyagedifiantDashboard\listeVehiculeController;
use App\Http\Controllers\VoyagedifiantDashboard\LoginController;
use App\Http\Controllers\VoyagedifiantDashboard\NewPasswordController;
use App\Http\Controllers\VoyagedifiantDashboard\PasswordResetLinkController;
use App\Http\Controllers\VoyagedifiantDashboard\ResetPasswordController;
use App\Http\Controllers\VoyagedifiantDashboard\ShowChauffeurController;
use App\Http\Controllers\VoyagedifiantDashboard\ShowHotelController;
use App\Http\Controllers\VoyagedifiantDashboard\ShowSiteController;
use App\Http\Controllers\VoyagedifiantDashboard\ShowUtilisateurController;
use App\Http\Controllers\VoyagedifiantDashboard\ShowVehiculeController;
use App\Http\Controllers\VoyagedifiantDashboard\SiteController;
use App\Http\Controllers\VoyagedifiantDashboard\UtilisateurController;
use App\Http\Controllers\VoyagedifiantDashboard\VehiculeController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

// Route::get('/', function () {
//     return view('welcome');
// });
// Routes pour visiteurs non connectés
Route::middleware(['guest'])->group(function () {
Route::get('/', [LoginController::class, 'showlogin'])->name('login');
Route::post('/', [LoginController::class, 'connexion']);
Route::get('/forgot-password', [PasswordResetLinkController::class, 'create'])
    ->middleware('guest')
    ->name('password.request'); // Le nom DOIT être 'password.request'

    // Traitement email
    Route::post('/forgot-password', [PasswordResetLinkController::class, 'store'])
        ->name('password.email');

    // Formulaire de réinitialisation
    Route::get('/reset-password/{token}', [NewPasswordController::class, 'create'])
        ->name('password.reset');

    // Mise à jour mot de passe
    Route::post('/reset-password', [NewPasswordController::class, 'store'])
        ->name('password.update');
});

// Déconnexion (accessible aux connectés)
Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

// Routes protégées (nécessitent une connexion)
Route::middleware(['auth'])->group(function () {
    Route::get('/dashboard', [IndexController::class, 'showIndex'])->name('dashboard');
Route::get('/listeHotel', [listeHotelController::class, 'showlisteHotel'])->name('listeHotel');
Route::get('/listeConducteur', [listeConducteurController::class, 'showlisteConducteur'])->name('listeConducteur');
Route::get('/listeVehicule', [listeVehiculeController::class, 'showlisteVehicule'])->name('listeVehicule');
Route::get('/listeUtilisateur', [listeUtilisateurController::class, 'showlisteUtilisateur'])->name('listeUtilisateur');
Route::get('/listeSite', [listedeSiteController::class, 'showlistedeSite'])->name('listeSite');

Route::get('/listeVehicule', [listeVehiculeController::class, 'showlisteVehicule'])->name('listeVehicule');
Route::get('/formVehicule', [formVehiculeController::class, 'showformVehicule'])->name('formVehicule');
Route::post('/formVehicule', [FormVehiculeController::class, 'formVehicule'])->name('formVehicule.submit');
Route::get('/dashboard/showvehicule/{id}', [ShowVehiculeController::class, 'show'])->name('dashboard.showvehicule');
Route::get('/ordervehicule/{id}/pdf', [IndexController::class, 'generatPdf'])->name('ordervehicule.pdf');
Route::get('/vehicules/{vehicule}/edit', [VehiculeController::class, 'edit'])->name('formeditVehicule');
Route::put('/vehicules/{vehicule}', [VehiculeController::class, 'update'])->name('vehicules.update');
Route::delete('/vehicule/{vehicule}', [VehiculeController::class, 'destroy'])->name('vehicules.destroy');

Route::get('/formHotel', [formHotelController::class, 'showformHotel'])->name('formHotel');
Route::post('/formHotel', [FormHotelController::class, 'formHotel'])->name('formHotel.submit');
Route::get('/dashboard/showhotel/{id}', [ShowHotelController::class, 'show'])->name('dashboard.showhotel');
Route::get('/hotels/{hotel}/edit', [HotelController::class, 'edit'])->name('formeEditHotel');
Route::put('/hotels/{hotel}', [HotelController::class, 'update'])->name('hotels.update');
Route::delete('/hotel/{hotel}', [HotelController::class, 'destroy'])->name('hotels.destroy');
Route::get('/hotelorder/{id}/pdf', [IndexController::class, 'generaPdf'])->name('hotelorder.pdf');

Route::get('/listeConducteur', [listeConducteurController::class, 'showlisteConducteur'])->name('listeConducteur');
Route::get('/formConducteur', [formConducteurController::class, 'showformConducteur'])->name('formConducteur');
Route::post('/formConducteur', [formConducteurController::class, 'formConducteur'])->name('formConducteur.submit');
Route::get('/conducteurs/{conducteur}/edit', [ChauffeurController::class, 'edit'])->name('formeditConducteur');
Route::put('/chauffeurs/{chauffeur}', [ChauffeurController::class, 'update'])->name('chauffeurs.update');
Route::delete('/chauffeur/{chauffeur}', [ChauffeurController::class, 'destroy'])->name('chauffeurs.destroy');
Route::get('/dashboard/showchauffeur/{id}', [ShowChauffeurController::class, 'show'])->name('dashboard.showchauffeur');

Route::get('/listeSite', [listedeSiteController::class, 'showlistedeSite'])->name('listeSite');
Route::get('/formSite', [formeSiteController::class, 'showForm'])->name('formSite');
Route::post('/formSite', [formeSiteController::class, 'store'])->name('formSite.store');
Route::get('/discoveryorder/{id}/pdf', [IndexController::class, 'generatePdf'])->name('discoveryorder.pdf');
Route::get('/sites/{site}/edit', [SiteController::class, 'edit'])->name('formeEditSite');
Route::put('/sites/{site}', [SiteController::class, 'update'])->name('sites.update');
Route::delete('/site{site}', [SiteController::class, 'destroy'])->name('sites.destroy');
Route::get('/dashboard/showsite/{id}', [ShowSiteController::class, 'show'])->name('dashboard.showsite');

Route::get('/listeUtilisateur', [listeUtilisateurController::class, 'showlisteUtilisateur'])->name('listeUtilisateur');
Route::get('/formUtilisateur', [formUtilisateurController::class, 'showformUtilisateur'])->name('formUtilisateur');
Route::post('/formUtilisateur', [formUtilisateurController::class, 'formUtlisateur'])->name('formUtilisateur');
Route::get('/utilisateur/{utilisateur}/edit', [UtilisateurController::class, 'edit'])->name('formeditUtilisateur');
Route::put('/utilisateurs/{utilisateur}', [UtilisateurController::class, 'update'])->name('voyagedifiantUsers.update');
Route::delete('/utilisateurs/{utilisateur}', [UtilisateurController::class, 'destroy'])->name('voyagedifiantUsers.destroy');
Route::get('/dashboard/showutilisateur/{id}', [ShowUtilisateurController::class, 'show'])->name('dashboard.showutilisateur');

// Route::post('mot-de-passe/email', [ForgotPasswordController::class, 'sendResetLinkEmail'])
//      ->name('password.request');;

// Route::get('mot-de-passe/reinitialiser/{token}', [ResetPasswordController::class, 'showResetForm'])
//      ->name('password.reset');

// Route::post('mot-de-passe/reinitialiser', [ResetPasswordController::class, 'reset'])
//      ->name('password.update');

// Route pour afficher le formulaire de demande de réinitialisation (GET)
Route::get('formotdepasseoublie', [ForgotPasswordController::class, 'reset'])->name('formotdepasseoublie');

// Route pour traiter l'envoi d'email (POST)
Route::post('mot-de-passe/email', [ForgotPasswordController::class, 'sendResetLinkEmail'])
     ->name('password.email');

// // Route pour afficher le formulaire de réinitialisation (GET)
// Route::get('mot-de-passe/reinitialiser/{token}', [ResetPasswordController::class, 'showResetForm'])
//      ->name('password.reset');

// // Route pour traiter la réinitialisation (POST)
// Route::post('mot-de-passe/reinitialiser', [ResetPasswordController::class, 'reset'])
//      ->name('password.update');

Route::get('/dashboard/commandes/sites/{id}', [IndexController::class, 'showDetail'])
     ->name('dashboard.commandes.sites.show');

// Pour les commandes d'hôtels
Route::get('/dashboard/commandes/hotels/{id}', [IndexController::class, 'detailshow'])
     ->name('dashboard.commandes.hotels.show');

// Pour les commandes de véhicules (si nécessaire)
Route::get('/dashboard/commandes/vehicules/{id}', [IndexController::class, 'showVehicleDetail'])
     ->name('dashboard.commandes.vehicules.show');


     // Route pour effacer le flag de session
// Route::post('/clear-session-flag', function() {
//     session()->forget('vehicle_viewed');
//     return response()->json(['success' => true]);
// })->name('clear.session.flag');

// // Route pour marquer une commande comme vue via AJAX
// Route::post('/vehicle-order/{id}/mark-viewed', [IndexController::class, 'markVehicleAsViewed'])
//     ->name('vehicle.order.mark.viewed');
//
 });


 // Routes de notifications (dans web.php)


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

    // Compter les notifications
    Route::get('/notifications/count', function() {
        $count = \App\Models\DiscoveryOrders::where('is_viewed', false)->count()
               + \App\Models\HotelOrder::where('is_viewed', false)->count()
               + \App\Models\VehicleReservation::where('is_viewed', false)->count();

        return response()->json(['count' => $count]);
    });

    // Récupérer les notifications détaillées
    Route::get('/notifications', function() {
        $discoveryOrders = \App\Models\DiscoveryOrders::where('is_viewed', false)
            ->select('id', 'site_name as name', 'created_at')
            ->get()
            ->map(function($order) {
                $order->type = 'discovery';
                return $order;
            });

        $hotelOrders = \App\Models\HotelOrder::where('is_viewed', false)
            ->select('id', 'hotel_name as name', 'created_at')
            ->get()
            ->map(function($order) {
                $order->type = 'hotel';
                return $order;
            });

        $vehicleOrders = \App\Models\VehicleReservation::where('is_viewed', false)
            ->select('id', 'vehicle_name as name', 'created_at')
            ->get()
            ->map(function($order) {
                $order->type = 'vehicle';
                return $order;
            });

        $notifications = $discoveryOrders->concat($hotelOrders)->concat($vehicleOrders)
            ->sortByDesc('created_at')
            ->values(); // Réindexer le array

        return response()->json(['notifications' => $notifications]);
    });

    // Effacer toutes les notifications
    Route::post('/notifications/clear', function() {
        \App\Models\DiscoveryOrders::where('is_viewed', false)->update(['is_viewed' => true]);
        \App\Models\HotelOrder::where('is_viewed', false)->update(['is_viewed' => true]);
        \App\Models\VehicleReservation::where('is_viewed', false)->update(['is_viewed' => true]);

        return response()->json(['success' => true]);
    });

    // Routes pour marquer comme vu
    // Route::post('/discovery-orders/{id}/mark-viewed', [IndexController::class, 'markDiscoveryAsViewed']);
    // Route::post('/hotel-orders/{id}/mark-viewed', [IndexController::class, 'markHotelAsViewed']);
    // Route::post('/vehicle-orders/{id}/mark-viewed', [IndexController::class, 'markVehicleAsViewed']);

});

