<?php

namespace App\Http\Controllers\VoyagedifiantDashboard;

use App\Events\NewOrderNotification;
use App\Events\OrderViewedNotification;
use App\Http\Controllers\Controller;
use App\Models\Chauffeur;
use App\Models\DiscoveryOrders;
use App\Models\Hotel;
use App\Models\HotelOrder;
use App\Models\Site;
use App\Models\VehicleReservation;
use App\Models\Vehicule;
use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade\Pdf;

class IndexController extends Controller
{


public function showIndex()
    {
        // Vérification que l'utilisateur est connecté
        if (!auth()->check()) {
            return redirect()->route('login');
        }


        $counts = [
            'vehicules' => Vehicule::count(),
            'sites' => Site::count(),
            'chauffeurs' => Chauffeur::count(),
            'hotels' => Hotel::count()
        ];

        // Récupération des commandes
        $discoveryorders = DiscoveryOrders::all();
        $hotelorders = HotelOrder::all();
        $vehiclereservations = VehicleReservation::all();

         // DEBUG: Vérifier les statuts des véhicules
    \Illuminate\Support\Facades\Log::info("=== DASHBOARD VÉHICULES ===");
    foreach ($vehiclereservations as $index => $vehicule) {
        \Illuminate\Support\Facades\Log::info("Véhicule " . ($index + 1) . ": " . $vehicule->vehicle_name . " - is_viewed: " . ($vehicule->is_viewed ? 'true' : 'false'));
    }

        return view('page.dashboard', compact(
            'counts',
            'discoveryorders',
            'hotelorders',
            'vehiclereservations'
        ));
    }


    public function showDetail($id)
    {
        $discoveryorder = DiscoveryOrders::findOrFail($id);

        if (!$discoveryorder->is_viewed) {
            $discoveryorder->update(['is_viewed' => true]);
            // event(new OrderViewedNotification($discoveryorder->id, 'discovery'));

        }

        return view('page.commande-details-de-site', compact('discoveryorder'));
    }

    // Pour les hôtels
    public function detailshow($id)
    {
        $hotelorder = HotelOrder::findOrFail($id);

        if (!$hotelorder->is_viewed) {
            $hotelorder->update(['is_viewed' => true]);
            // event(new OrderViewedNotification($hotelorder->id, 'hotel'));

        }

        return view('page.commande-details-de-hotel', compact('hotelorder'));
    }

    // Pour les véhicules
    // public function showVehicleDetail($id)
    // {
    //     $ordervehicule = VehicleReservation::findOrFail($id);

    //     if (!$ordervehicule->is_viewed) {
    //         $ordervehicule->update(['is_viewed' => true]);
    //     }

    //     return view('page.commande-details-de-vehicule', compact('ordervehicule'));
    // }

 public function showVehicleDetail($id)
{
    \Illuminate\Support\Facades\Log::info("=== DÉBUT showVehicleDetail ===");
    \Illuminate\Support\Facades\Log::info("ID véhicule: " . $id);

    $vehiclereservation = VehicleReservation::findOrFail($id);
    \Illuminate\Support\Facades\Log::info("Avant mise à jour - is_viewed: " . ($vehiclereservation->is_viewed ? 'true' : 'false'));

    if (!$vehiclereservation->is_viewed) {
        // SOLUTION 1 : Utiliser save() au lieu de update()
        $vehiclereservation->is_viewed = true;
        $vehiclereservation->save(); // ← CHANGEMENT ICI

        \Illuminate\Support\Facades\Log::info("Mise à jour effectuée avec SAVE");

        // Recharger l'instance depuis la base
        $vehiclereservation->refresh();
        \Illuminate\Support\Facades\Log::info("Après refresh - is_viewed: " . ($vehiclereservation->is_viewed ? 'true' : 'false'));
    } else {
        \Illuminate\Support\Facades\Log::info("Déjà vu - pas de mise à jour nécessaire");
    }

    \Illuminate\Support\Facades\Log::info("=== FIN showVehicleDetail ===");

    return view('page.commande-details-de-vehicule', compact('vehiclereservation'));
}


    public function generatePdf($id)

    {
    $discoveryorder = DiscoveryOrders::findOrFail($id); // Enlever ->with('images')

    $imagePath = null;
    if ($discoveryorder->discovery_image) {
        $fullPath = storage_path('app/public/' . $discoveryorder->discovery_image);
        if (file_exists($fullPath)) {
            $imagePath = $fullPath;
        }
    }

    $pdf = PDF::loadView('pdf.discoveryorder', [
        'discoveryorder' => $discoveryorder,
        'imagePath' => $imagePath,
    ]);

    return $pdf->stream('discoveryorder-' . $discoveryorder->site_name . '.pdf');
}



public function generaPdf($id)

    {
    $hotelorder = HotelOrder::findOrFail($id); // Enlever ->with('images')

    $imagePath = null;
    if ($hotelorder->discovery_image) {
        $fullPath = storage_path('app/public/' . $hotelorder->hotel_image);
        if (file_exists($fullPath)) {
            $imagePath = $fullPath;
        }
    }

    $pdf = PDF::loadView('pdf.hotelorder', [
        'hotelorder' => $hotelorder,
        'imagePath' => $imagePath,
    ]);

    return $pdf->stream('hotelorder-' . $hotelorder->hotel_name . '.pdf');
}


public function generatPdf($id)
{
    $ordervehicule = VehicleReservation::findOrFail($id);

    $imagePath = null;
    if ($ordervehicule->ordervehicule_image) { // Vérifiez le nom exact de votre colonne
        $fullPath = storage_path('app/public/' . $ordervehicule->ordervehicule_image);
        if (file_exists($fullPath)) {
            $imagePath = $fullPath;
        }
    }

    $pdf = PDF::loadView('pdf.ordervehicule', [
        'ordervehicule' => $ordervehicule, // Vous passez 'vehicleorder'
        'imagePath' => $imagePath,
    ]);

    return $pdf->stream('ordervehicule-' . $ordervehicule->id . '.pdf');
}

}
// }
