vehicleRepository->paginate( (int) $request->integer('per_page', 15), $request->only(['search', 'status', 'vehicle_type', 'sort_by', 'sort_direction']) ); return response()->json([ 'data' => VehicleResource::collection($vehicles->items()), 'meta' => [ 'current_page' => $vehicles->currentPage(), 'last_page' => $vehicles->lastPage(), 'per_page' => $vehicles->perPage(), 'total' => $vehicles->total(), ], 'status' => 'success', ]); } catch (\Exception $e) { Log::error('Error fetching vehicles: ' . $e->getMessage()); return response()->json([ 'message' => 'An error occurred while fetching vehicles.', 'error' => config('app.debug') ? $e->getMessage() : null, ], 500); } } public function store(StoreVehicleRequest $request): JsonResponse { try { $vehicle = $this->vehicleRepository->create($request->validated()); return response()->json([ 'data' => new VehicleResource($vehicle->load('primaryUser')), 'message' => 'Vehicle created successfully.', 'status' => 'success', ], 201); } catch (\Exception $e) { Log::error('Error creating vehicle: ' . $e->getMessage()); return response()->json([ 'message' => 'An error occurred while creating the vehicle.', 'error' => config('app.debug') ? $e->getMessage() : null, ], 500); } } public function show(string $id): JsonResponse { try { $vehicle = $this->vehicleRepository->find((int) $id); if (! $vehicle) { return response()->json(['message' => 'Vehicle not found.'], 404); } $vehicle->load(['primaryUser', 'convoys']); return response()->json([ 'data' => new VehicleResource($vehicle), 'status' => 'success', ]); } catch (\Exception $e) { Log::error('Error fetching vehicle: ' . $e->getMessage()); return response()->json([ 'message' => 'An error occurred while fetching the vehicle.', 'error' => config('app.debug') ? $e->getMessage() : null, ], 500); } } public function update(UpdateVehicleRequest $request, string $id): JsonResponse { try { $updated = $this->vehicleRepository->update((int) $id, $request->validated()); if (! $updated) { return response()->json(['message' => 'Vehicle not found or update failed.'], 404); } $vehicle = $this->vehicleRepository->find((int) $id); return response()->json([ 'data' => new VehicleResource($vehicle->load('primaryUser')), 'message' => 'Vehicle updated successfully.', 'status' => 'success', ]); } catch (\Exception $e) { Log::error('Error updating vehicle: ' . $e->getMessage()); return response()->json([ 'message' => 'An error occurred while updating the vehicle.', 'error' => config('app.debug') ? $e->getMessage() : null, ], 500); } } public function destroy(string $id): JsonResponse { try { $deleted = $this->vehicleRepository->delete((int) $id); if (! $deleted) { return response()->json(['message' => 'Vehicle not found or delete failed.'], 404); } return response()->json([ 'message' => 'Vehicle deleted successfully.', 'status' => 'success', ]); } catch (\Exception $e) { Log::error('Error deleting vehicle: ' . $e->getMessage()); return response()->json([ 'message' => 'An error occurred while deleting the vehicle.', 'error' => config('app.debug') ? $e->getMessage() : null, ], 500); } } }