<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Fleet;

class FleetController extends Controller
{
    public function index()
    {
        $fleets = Fleet::all();
        return view('fleet.index', compact('fleets'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'vehicle_number' => 'required|unique:fleets',
            'driver_name' => 'required',
            'vehicle_type' => 'required',
        ]);

        Fleet::create($request->all());

        return redirect()->back()->with('success', 'Vehicle registered successfully.');
    }

    public function update(Request $request, Fleet $fleet)
    {
        $request->validate([
            'vehicle_number' => 'required|unique:fleets,vehicle_number,' . $fleet->id,
            'driver_name' => 'required',
            'vehicle_type' => 'required',
        ]);

        $fleet->update($request->all());

        return redirect()->back()->with('success', 'Vehicle updated successfully.');
    }

    public function destroy(Fleet $fleet)
    {
        $fleet->delete();
        return redirect()->back()->with('success', 'Vehicle deleted successfully.');
    }
}
