validate([ 'roles' => 'required|array', 'roles.*' => 'exists:roles,id,guard_name,api', ]); $result = $this->userService->assignRoles($id, $request->roles); if (isset($result['error'])) { $status = $result['error'] === 'User not found.' ? 404 : 400; return response()->json([ 'success' => false, 'message' => $result['error'], ], $status); } return response()->json([ 'success' => true, 'message' => 'Roles assigned successfully.', 'data' => (new UserResource($result['user']))->resolve(), ]); } public function updateProfile(Request $request): JsonResponse { $user = $request->user(); $validated = $request->validate([ 'name' => 'sometimes|string|max:255', 'ic_number' => 'sometimes|string|max:255|unique:users,ic_number,'.$user->id, 'position' => 'sometimes|string|max:255', 'phone_number' => 'sometimes|string|max:255', 'image' => 'sometimes|image|mimes:jpeg,png,jpg,gif|max:2048', 'image_url' => 'sometimes|string|max:255', ]); $user = $this->userService->updateProfile( $user, $validated, $request->file('image') ); return response()->json([ 'success' => true, 'data' => new UserResource($user), 'message' => 'Profile updated successfully.', ]); } public function updatePassword(Request $request): JsonResponse { $user = $request->user(); $validated = $request->validate([ 'current_password' => 'required|string', 'password' => 'required|string|min:8|confirmed', ]); $result = $this->userService->updatePassword( $user, $validated['current_password'], $validated['password'] ); if (isset($result['error'])) { return response()->json([ 'success' => false, 'message' => $result['error'], ], 400); } return response()->json([ 'success' => true, 'message' => 'Password updated successfully.', ]); } public function index(Request $request): JsonResponse { $this->authorize('viewAny', $this->modelClass); try { $perPage = min((int) $request->get('per_page', 10), 500); $items = $this->userService->getPaginatedList( $perPage, $request->get('search', ''), $request->get('status', ''), $request->get('sort_by', 'id'), $request->get('sort_order', 'asc') ); return response()->json([ 'success' => true, 'data' => UserListResource::collection($items->items()), 'pagination' => [ 'current_page' => $items->currentPage(), 'per_page' => $items->perPage(), 'total' => $items->total(), 'last_page' => $items->lastPage(), 'from' => $items->firstItem(), 'to' => $items->lastItem(), 'has_more_pages' => $items->hasMorePages(), ], 'message' => $this->getSuccessMessage('index'), ]); } catch (Exception $e) { Log::error("Error fetching {$this->resourceNamePlural}: ".$e->getMessage()); return $this->errorResponse($this->getErrorMessage('index'), 500); } } public function show($id): JsonResponse { $this->authorize('view', $this->modelClass); try { $user = $this->userService->getUserWithRelations($id); if (! $user) { return response()->json([ 'success' => false, 'message' => 'User not found.', ], 404); } return response()->json([ 'success' => true, 'data' => new UserResource($user), 'message' => 'User retrieved successfully.', ]); } catch (Exception $e) { Log::error('Error fetching user: '.$e->getMessage()); return $this->errorResponse('Failed to retrieve user.', 500); } } }