public function getPpiByDemographicQuestion(Collection $questions, ?string $code): Collection 	{ 		$question = $questions->firstWhere('code', $code);  		if (!$question) { 			return collect(); 		}  		$top_companies_limit = 10;  		$available_answers = empty($question->getTranslation('available_answers', $this->locale)) ? $question->getDefaultTranslation('available_answers') : $question->getTranslation('available_answers', $this->locale);  		$companies_by_answer = [];  		foreach ($available_answers as $answer_value => $answer_translation) { 			$applicant_ids = $this->client_survey->applicants() 			->whereHas('companies', function ($query) { 				$query->byIndustry($this->industry_name)->available(); 			}) 			->available() 			->final() 			->whereHas('answers', function ($query) use ($question, $answer_value) { 				$query 				->where('question_id', $question->id) 				->where('answer_value', $answer_value + 1); 			})->pluck('id');  			$companies_by_answer[$answer_value] = Company::byIndustry($this->industry_name) 			->byDomain($this->domain) 			->whereHas('applicants') 			->available() 			->withCount([ 				'applicantCompanyPivot as first_place_ranking_count' => function ($query) use ($applicant_ids) { 					$query->where('rank', 1)->whereIn('applicant_id', $applicant_ids); 				}, 				'applicantCompanyPivot as second_place_ranking_count' => function ($query) use ($applicant_ids) { 					$query->where('rank', 2)->whereIn('applicant_id', $applicant_ids); 				}, 				'applicantCompanyPivot as third_place_ranking_count' => function ($query) use ($applicant_ids) { 					$query->where('rank', 3)->whereIn('applicant_id', $applicant_ids); 				}, 			]) 			->orderByRaw('((first_place_ranking_count * 3) + (second_place_ranking_count * 2) + third_place_ranking_count) DESC') 			->limit($top_companies_limit) 			->get(); 		}  		$merged = [];  		if ($question->type === QuestionType::CustomRadio->value && $question->class === QuestionClass::PpiDemographic) { 			foreach ($companies_by_answer as $index => $companies) { 				$question->questionAnswerGroups->each(function (QuestionAnswerGroup $questionAnswerGroups) use (&$merged, $index, &$companies, &$companies_by_answer) { 					if (in_array($index + 1, $questionAnswerGroups->grouped_values)) { 						$merged[$questionAnswerGroups->getTranslation('title', $this->locale)] = array_merge($merged[$questionAnswerGroups->getTranslation('title', $this->locale)] ?? [], $companies->all()); 						unset($companies_by_answer[$index]); 					} 				}); 			} 		}  		$translated_companies_by_answer = [];  		array_walk( 			$companies_by_answer, 			function ($value, $key) use ($available_answers, &$translated_companies_by_answer) { 				$translated_key = $available_answers[$key] ?? ($key);  				return $translated_companies_by_answer[$translated_key] = $value; 			} 		);  		$companies_duplicate_filtered = collect();  		foreach ($merged as $grouped_value_key => $value) { 			$company_collection_holder = collect($value); 			$company_collection_holder->groupBy('id')->each(function (Collection $companies_to_be_merged) use ($grouped_value_key, &$companies_duplicate_filtered) { 				$company_grouped_collection = collect([]);  				$companies_to_be_merged->each(function (Company $company) use (&$company_grouped_collection) { 					if (!isset($company_grouped_collection[$company->getKey()])) { 						$company_grouped_collection[$company->getKey()] = $company; 					} else { 						$company_grouped_collection[$company->getKey()]->first_place_ranking_count += $company->first_place_ranking_count; 						$company_grouped_collection[$company->getKey()]->second_place_ranking_count += $company->second_place_ranking_count; 						$company_grouped_collection[$company->getKey()]->third_place_ranking_count += $company->third_place_ranking_count; 					} 				});  				if (isset($companies_duplicate_filtered[$grouped_value_key])) { 					$companies_duplicate_filtered[$grouped_value_key] = $companies_duplicate_filtered[$grouped_value_key]->merge($company_grouped_collection); 				} else { 					$companies_duplicate_filtered[$grouped_value_key] = $company_grouped_collection; 				} 			}); 		}  		$companies_duplicate_filtered = $companies_duplicate_filtered->map(function (Collection $companies) use ($top_companies_limit) { 			return $companies->sortByDesc(function (Company $company) { 				return $company['first_place_ranking_count'] * 3 					+ $company['second_place_ranking_count'] * 2 					+ $company['third_place_ranking_count']; 			})->take($top_companies_limit); 		});  		$companies = $companies_duplicate_filtered->merge($translated_companies_by_answer);  		return collect($companies); 	}