check() && !Auth::guard('organization')->check() && !Auth::guard('bank')->check() && !Auth::guard('admin')->check()) { abort(403, 'Unauthorized'); } // Map profile type to model class $modelClass = match(strtolower($profileType)) { 'user' => User::class, 'organization' => Organization::class, 'bank' => Bank::class, 'admin' => Admin::class, default => abort(404, 'Invalid profile type'), }; // Find the recipient profile $recipient = $modelClass::find($id); if (!$recipient) { abort(404, 'Profile not found'); } // Check if recipient is removed if (method_exists($recipient, 'isRemoved') && $recipient->isRemoved()) { abort(404, 'Profile no longer available'); } // Get the active profile using the helper function $activeProfile = getActiveProfile(); // Prevent creating conversation with yourself if ($activeProfile->id === $recipient->id && get_class($activeProfile) === get_class($recipient)) { return redirect()->route('chats')->with('error', __('You cannot start a conversation with yourself.')); } // Create or get existing conversation $conversation = $activeProfile->createConversationWith($recipient); // Redirect to the chat page return redirect()->route('chat', ['conversation' => $conversation->id]); } }