<?php

namespace App\Http\Controllers;

use App\Models\Booking;
use App\Models\Vehicle;
use Illuminate\Http\Request;

class BookingController extends Controller
{
    public function index()
    {
        $vehicles = Vehicle::where('is_available', true)->latest()->get();
        return view('bookings.index', compact('vehicles'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|max:255',
            'phone' => 'required|string|max:20',
            'pickup_date' => 'required|date|after_or_equal:today',
            'pickup_time' => 'required',
            'dropoff_date' => 'required|date|after_or_equal:pickup_date',
            'dropoff_time' => 'required',
            'vehicle_id' => 'required|exists:vehicles,id',
            'special_requests' => 'nullable|string|max:1000',
        ]);

        $booking = Booking::create([
            'name' => $request->name,
            'email' => $request->email,
            'phone' => $request->phone,
            'pickup_date' => $request->pickup_date,
            'pickup_time' => $request->pickup_time,
            'dropoff_date' => $request->dropoff_date,
            'dropoff_time' => $request->dropoff_time,
            'vehicle_id' => $request->vehicle_id,
            'special_requests' => $request->special_requests,
            'status' => 'pending'
        ]);

        return redirect()->route('bookings.index')->with('success', 'Booking submitted successfully! We will contact you soon to confirm your reservation.');
    }
}
