<?php

namespace App\Http\Controllers\Api;

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

class HotelOrderApiController extends Controller
{
    public function store(Request $request)
{
    $validated = $request->validate([
        'hotel_name' => 'required|string',
        'hotel_image' => 'required|image|mimes:jpeg,png,jpg|max:2048',
        'driver_name' => 'nullable|string',
        'city' => 'required|string',
        'reservation_period' => 'required|string',
        'price' => 'required|numeric',
        'classe' => 'required|string',
        'total_price' => 'required|numeric',
        'amount_paid' => 'required|numeric',
        'username' => 'required|string',
        'phone' => 'required|string',
        'invoice_number' => 'required|string',
        'user_id' => 'required|exists:users,id',
    ]);

    // Génération du numéro de facture
    $validated['invoice_number'] = 'FACT-' . date('Ymd') . '-' . strtoupper(uniqid());

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

    // Création de la commande
    $order = HotelOrder::create($validated);

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