attributes->get('worker'); $worker->load(['category', 'skills', 'documents']); return response()->json([ 'success' => true, 'data' => [ 'worker' => $worker ] ], 200); } /** * Update authorized worker's profile details. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ public function updateProfile(Request $request) { /** @var Worker $worker */ $worker = $request->attributes->get('worker'); $validator = Validator::make($request->all(), [ 'name' => 'nullable|string|max:255', 'age' => 'nullable|integer|min:18|max:70', 'nationality' => 'nullable|string|max:100', 'language' => 'nullable|string', 'salary' => 'nullable|numeric|min:0', 'availability' => 'nullable|string|max:100', 'experience' => 'nullable|string|max:100', 'religion' => 'nullable|string|max:100', 'bio' => 'nullable|string|max:1000', 'category_id' => 'nullable|exists:worker_categories,id', 'skills' => 'nullable|array', 'skills.*' => 'exists:skills,id', 'in_country' => 'nullable', 'visa_status' => 'nullable|string|max:100', 'preferred_job_type' => 'nullable|string|max:100', 'gender' => 'nullable|string|in:male,female,other', 'live_in_out' => 'nullable|string|in:live_in,live_out', 'preferred_location' => 'nullable|string|max:255', 'country' => 'nullable|string|max:100', 'city' => 'nullable|string|max:100', 'area' => 'nullable|string|max:100', 'fcm_token' => 'nullable|string|max:255', ]); if ($validator->fails()) { return response()->json([ 'success' => false, 'message' => 'Validation error.', 'errors' => $validator->errors() ], 422); } try { DB::transaction(function () use ($request, $worker) { // Update worker attributes $updateData = $request->only([ 'name', 'age', 'nationality', 'language', 'salary', 'availability', 'experience', 'religion', 'bio', 'category_id', 'visa_status', 'preferred_job_type', 'gender', 'live_in_out', 'preferred_location', 'country', 'city', 'area', 'fcm_token' ]); // Filter out null inputs if we want to support partial updates $updateData = array_filter($updateData, function ($value) { return !is_null($value); }); if ($request->has('in_country')) { $updateData['in_country'] = filter_var($request->input('in_country'), FILTER_VALIDATE_BOOLEAN); } $worker->update($updateData); // Sync skills if provided if ($request->has('skills')) { $worker->skills()->sync($request->skills ?: []); } }); $worker->load(['category', 'skills', 'documents']); return response()->json([ 'success' => true, 'message' => 'Profile updated successfully.', 'data' => [ 'worker' => $worker ] ], 200); } catch (\Exception $e) { logger()->error('Mobile Worker Profile Update Failure: ' . $e->getMessage()); return response()->json([ 'success' => false, 'message' => 'An error occurred while updating your profile.', 'error' => app()->environment('local') ? $e->getMessage() : 'Internal Server Error' ], 500); } } /** * S3: Profile Go Live (Status: Active). * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ public function goLive(Request $request) { /** @var Worker $worker */ $worker = $request->attributes->get('worker'); try { $worker->update([ 'status' => 'active', 'availability' => 'Immediate' ]); return response()->json([ 'success' => true, 'message' => 'Your profile is now live! Status set to Active.', 'data' => [ 'worker' => $worker->load(['category', 'skills', 'documents']) ] ], 200); } catch (\Exception $e) { logger()->error('Mobile Worker Go Live Failure: ' . $e->getMessage()); return response()->json([ 'success' => false, 'message' => 'Failed to go live. Please try again.' ], 500); } } /** * S5: Toggle availability - "Still looking for job?". * If YES -> Visible (status: active), If NO -> Hidden (status: hidden). * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ public function toggleAvailability(Request $request) { /** @var Worker $worker */ $worker = $request->attributes->get('worker'); $validator = Validator::make($request->all(), [ 'still_looking' => 'required|boolean', ]); if ($validator->fails()) { return response()->json([ 'success' => false, 'message' => 'Validation error.', 'errors' => $validator->errors() ], 422); } try { $stillLooking = (bool) $request->still_looking; $newStatus = $stillLooking ? 'active' : 'hidden'; $worker->update([ 'status' => $newStatus, 'availability' => $stillLooking ? 'Immediate' : 'Not Available' ]); return response()->json([ 'success' => true, 'message' => $stillLooking ? 'Your profile is now Visible in search results.' : 'Your profile has been Hidden from search results.', 'still_looking' => $stillLooking, 'status' => $newStatus, 'data' => [ 'worker' => $worker->load(['category', 'skills', 'documents']) ] ], 200); } catch (\Exception $e) { logger()->error('Mobile Worker Toggle Availability Failure: ' . $e->getMessage()); return response()->json([ 'success' => false, 'message' => 'Failed to update availability status. Please try again.' ], 500); } } /** * S6: Marked as Hired (Profile removed from search - auto-hidden). * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ public function markHired(Request $request) { /** @var Worker $worker */ $worker = $request->attributes->get('worker'); try { $worker->update([ 'status' => 'Hired', 'availability' => 'Hired' ]); return response()->json([ 'success' => true, 'message' => 'Congratulations! You have been successfully marked as Hired. Your profile has been automatically removed from active search.', 'data' => [ 'worker' => $worker->load(['category', 'skills', 'documents']) ] ], 200); } catch (\Exception $e) { logger()->error('Mobile Worker Mark Hired Failure: ' . $e->getMessage()); return response()->json([ 'success' => false, 'message' => 'Failed to mark as hired. Please try again.' ], 500); } } /** * Get all job offers received by the worker. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ public function getOffers(Request $request) { /** @var Worker $worker */ $worker = $request->attributes->get('worker'); $offers = JobOffer::where('worker_id', $worker->id) ->with(['employer.employerProfile']) // Load employer and their profile details ->latest() ->get(); return response()->json([ 'success' => true, 'data' => [ 'offers' => $offers ] ], 200); } /** * Respond to a specific job offer (Accept or Reject). * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\JsonResponse */ public function respondToOffer(Request $request, $id) { /** @var Worker $worker */ $worker = $request->attributes->get('worker'); $validator = Validator::make($request->all(), [ 'status' => 'required|string|in:accepted,rejected', ], [ 'status.in' => 'Status must be either accepted or rejected.' ]); if ($validator->fails()) { return response()->json([ 'success' => false, 'message' => 'Validation error.', 'errors' => $validator->errors() ], 422); } try { $offer = JobOffer::where('id', $id) ->where('worker_id', $worker->id) ->first(); if (!$offer) { return response()->json([ 'success' => false, 'message' => 'Job offer not found.' ], 404); } if ($offer->status !== 'pending') { return response()->json([ 'success' => false, 'message' => 'This job offer has already been responded to.' ], 400); } $responseStatus = $request->status; DB::transaction(function () use ($offer, $worker, $responseStatus) { // Update offer status $offer->update(['status' => $responseStatus]); // If accepted, change worker status to 'Hired' if ($responseStatus === 'accepted') { $worker->update([ 'status' => 'Hired', 'availability' => 'Hired' ]); } }); // Dispatch push notification to employer $employer = $offer->employer; if ($employer && $employer->fcm_token) { $statusMessage = $responseStatus === 'accepted' ? 'accepted' : 'rejected'; \App\Services\FCMService::sendPushNotification( $employer->fcm_token, "Job Offer " . ucfirst($statusMessage), "Worker " . ($worker->name ?? "Candidate") . " has " . $statusMessage . " your job offer.", [ 'type' => 'job_offer_response', 'offer_id' => $offer->id, 'status' => $responseStatus, ] ); } return response()->json([ 'success' => true, 'message' => "Job offer successfully " . $responseStatus . ".", 'data' => [ 'offer' => $offer, 'worker_status' => $worker->fresh()->status ] ], 200); } catch (\Exception $e) { logger()->error('Mobile Respond to Offer Failure: ' . $e->getMessage()); return response()->json([ 'success' => false, 'message' => 'An error occurred while responding to the job offer.', 'error' => app()->environment('local') ? $e->getMessage() : 'Internal Server Error' ], 500); } } /** * Get profile view statistics and employer listing for the worker dashboard. * GET /api/workers/dashboard/views */ public function getProfileViews(Request $request) { /** @var Worker $worker */ $worker = $request->attributes->get('worker'); try { // Get views count $viewsCount = ProfileView::where('worker_id', $worker->id)->count(); // Get list of employers/sponsors who viewed, with their details $views = ProfileView::where('worker_id', $worker->id) ->with('employer') ->latest('updated_at') ->get(); $list = $views->map(function ($view) { $emp = $view->employer; $empProfile = null; if ($emp) { $empProfile = EmployerProfile::where('user_id', $emp->id)->first(); } return [ 'id' => $view->id, 'employer_id' => $view->employer_id, 'employer_name' => $emp->name ?? 'Employer/Sponsor', 'company_name' => $empProfile->company_name ?? 'Private Sponsor', 'nationality' => $empProfile->nationality ?? 'UAE', 'city' => $empProfile->city ?? 'Dubai', 'viewed_at' => $view->updated_at->toIso8601String(), 'viewed_at_formatted' => $view->updated_at->diffForHumans(), ]; }); return response()->json([ 'success' => true, 'data' => [ 'views_count' => $viewsCount, 'views_list' => $list ] ], 200); } catch (\Exception $e) { logger()->error('Worker Dashboard Views API Failure: ' . $e->getMessage()); return response()->json([ 'success' => false, 'message' => 'An error occurred while fetching profile views statistics.', 'error' => app()->environment('local') ? $e->getMessage() : 'Internal Server Error' ], 500); } } /** * Get consolidated worker dashboard details (Requirement: dashboard api). * GET /api/workers/dashboard */ public function getDashboard(Request $request) { /** @var Worker $worker */ $worker = $request->attributes->get('worker'); try { // 1. Active status $activeStatus = $worker->status; // 2. Count of sponsors who viewed the profile $viewsCount = ProfileView::where('worker_id', $worker->id)->count(); // Unique count of employers who viewed this profile $profileViewed = ProfileView::where('worker_id', $worker->id)->distinct('employer_id')->count('employer_id'); // Unique count of employers who contacted this worker $employerContacted = Conversation::where('worker_id', $worker->id)->distinct('employer_id')->count('employer_id'); // 3. Currently working sponsor/employer $currentSponsor = null; $acceptedOffer = JobOffer::where('worker_id', $worker->id) ->where('status', 'accepted') ->with('employer.employerProfile') ->first(); if ($acceptedOffer && $acceptedOffer->employer) { $emp = $acceptedOffer->employer; $empProfile = $emp->employerProfile; $currentSponsor = [ 'id' => $emp->id, 'name' => $emp->name, 'company_name' => $empProfile->company_name ?? 'Private Sponsor', 'nationality' => $empProfile->nationality ?? 'UAE', 'city' => $empProfile->city ?? 'Dubai', 'email' => $emp->email, 'phone' => $emp->phone, 'hired_at' => $acceptedOffer->updated_at->toIso8601String(), 'hired_at_formatted' => $acceptedOffer->updated_at->format('M d, Y'), ]; } // 4. Latest charity events / announcements list $charityEvents = Announcement::with(['employer.employerProfile', 'sponsor']) ->where('status', 'approved') ->latest() ->limit(5) ->get() ->map(function ($event) { $postedBy = 'System'; $organization = 'Migrant Support'; if ($event->sponsor_id) { $postedBy = $event->sponsor->full_name; $organization = $event->sponsor->organization_name; } elseif ($event->employer_id) { $postedBy = $event->employer->name; $organization = $event->employer->employerProfile->company_name ?? 'Employer'; } // Decode charity details if they exist in json $charityDetails = null; $content = $event->body; if (strpos($event->body, '{"type":"Charity"') === 0) { $decoded = json_decode($event->body, true); if ($decoded) { $charityDetails = $decoded; $content = $decoded['content'] ?? $event->body; } } return [ 'id' => $event->id, 'title' => $event->title, 'body' => $content, 'type' => $event->type, 'employer_name' => $postedBy, 'company_name' => $organization, 'created_at' => $event->created_at->toIso8601String(), 'time_ago' => $event->created_at->diffForHumans(), 'charity_details' => $charityDetails, ]; }); return response()->json([ 'success' => true, 'data' => [ 'active_status' => $activeStatus, 'count_sponsors_viewed' => $viewsCount, 'employer_contacted' => $employerContacted, 'profile_viewed' => $profileViewed, 'currently_working_sponsor' => $currentSponsor, 'latest_charity_events_list' => $charityEvents ] ], 200); } catch (\Exception $e) { logger()->error('Worker Dashboard API Failure: ' . $e->getMessage()); return response()->json([ 'success' => false, 'message' => 'An error occurred while loading the worker dashboard.', 'error' => app()->environment('local') ? $e->getMessage() : 'Internal Server Error' ], 500); } } }