<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\VehicleReservation;
use Illuminate\Http\Request;

class VehicleReservationApiController extends Controller
{
    public function store(Request $request)
{
    $validated = $request->validate([
        'vehicle_name' => 'required|string',
        'vehicle_image' => 'required|image|mimes:jpeg,png,jpg|max:2048', // Changé pour accepter une image
        'bags' => 'required|integer',
        'passengers' => 'required|integer',
        'air_conditioning' => 'required|boolean',
        'vehicle_class' => 'required|string',
        'lieuDePriseEnCharge' => 'required|string',
        'lieuDeRestitution' => 'required|string',
        'rental_period' => 'required|string',
        'driver' => 'required|boolean',
        'price' => 'required|numeric',
        'total_price' => 'required|numeric',
        'username' => 'required|string',
        'phone' => 'required|string',
        'amount_paid' => 'required|numeric',
        'invoice_number' => 'required|string',
        'user_id' => 'required|exists:users,id',
    ]);

    // Upload de l'image
    if ($request->hasFile('vehicle_image')) {
        $path = $request->file('vehicle_image')->store('vehicle_reservations', 'public');
        $validated['vehicle_image'] = $path;
    }

    $order = VehicleReservation::create($validated);

    return response()->json([
        'message' => 'Commande de véhicule enregistrée avec succès',
        'order' => $order
    ], 201);
}
}
