<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Vehicle;
use App\Http\Requests\StoreVehicleRequest;
use App\Http\Requests\UpdateVehicleRequest;

class VehicleController extends Controller
{
    public function index(Request $request)
    {
        if (auth()->user()->role != 'admin') {
            abort(403);
        }

        $search = $request->search;

        $vehicles = Vehicle::when($search, function ($query) use ($search) {
            $query->where('vehicle_name', 'like', "%$search%")
                ->orWhere('type', 'like', "%$search%")
                ->orWhere('fuel_type', 'like', "%$search%")
                ->orWhere('status', 'like', "%$search%");
        })->get();

        return view('admin.vehicles', compact('vehicles', 'search'));
    }

    public function create()
    {
        if (auth()->user()->role != 'admin') {
            abort(403);
        }

        return view('admin.create_vehicle');
    }

    public function store(StoreVehicleRequest $request)
    {
        if (auth()->user()->role != 'admin') {
            abort(403);
        }

        $imagePath = null;

        if ($request->hasFile('image')) {
            $imagePath = $request->file('image')->store('vehicles', 'public');
        }

        Vehicle::create([
            'image' => $imagePath,
            'vehicle_name' => $request->vehicle_name,
            'type' => $request->type,
            'year_model' => $request->year_model,
            'capacity' => $request->capacity,
            'fuel_type' => $request->fuel_type,
            'status' => $request->status,
        ]);

        return redirect('/admin/vehicles');
    }

    public function edit($id)
    {
        if (auth()->user()->role != 'admin') {
            abort(403);
        }

        $vehicle = Vehicle::findOrFail($id);

        return view('admin.edit_vehicle', compact('vehicle'));
    }

    public function update(Request $request, $id)
    {
        if (auth()->user()->role != 'admin') {
            abort(403);
        }

        $vehicle = Vehicle::findOrFail($id);

        $imagePath = $vehicle->image;

        if ($request->hasFile('image')) {
            $imagePath = $request->file('image')->store('vehicles', 'public');
        }

        $vehicle->update([
            'image' => $imagePath,
            'vehicle_name' => $request->vehicle_name,
            'type' => $request->type,
            'year_model' => $request->year_model,
            'capacity' => $request->capacity,
            'fuel_type' => $request->fuel_type,
            'status' => $request->status,
        ]);

        return redirect('/admin/vehicles');
    }

    public function destroy($id)
    {
        if (auth()->user()->role != 'admin') {
            abort(403);
        }

        $vehicle = Vehicle::findOrFail($id);

        $vehicle->delete();

        return redirect('/admin/vehicles');
    }

    public function maintenance($id)
{
    if (auth()->user()->role != 'admin') {
        abort(403);
    }

    $vehicle = Vehicle::findOrFail($id);

    $records = $vehicle->maintenanceRecords()
        ->latest()
        ->get();

    $totalCost = $records->sum('cost');

    return view('admin.vehicle-maintenance', compact(
        'vehicle',
        'records',
        'totalCost'
    ));
}
}