<?php  namespace App\Services\ClientSurveys;  use App\Models\Location; use App\Models\Question; use App\Models\Applicant; use App\Models\Department; use App\Enums\QuestionType; use App\Enums\QuestionClass; use App\Models\ClientSurvey; use App\Enums\DomainLanguages; use App\Exports\BasicReportExcel; use App\Http\Requests\GetReports; use Illuminate\Support\Collection; use Maatwebsite\Excel\Facades\Excel; use App\Exports\BasicReportStatisticExcel; use App\Services\Pdf\GeneralReport\Pages\EsiMetrics; use Symfony\Component\HttpFoundation\BinaryFileResponse; use App\Services\Applicants\IzzPdfEnpsCalculationService; use App\Services\Pdf\GeneralReport\DataObjects\EsiDataObject;  class ExportExcelService { 	protected string $locale; 	protected string $client_name; 	protected array $filter_values;  	public function __construct(public readonly ClientSurvey $client_survey, public readonly ClientSurveyService $client_survey_service, public readonly GetReports $request, public readonly EsiMetrics $esiService){}  	public static function getQuestionAnswersWithTitleAndScore(Collection $questions_answers, ClientSurvey $client_survey, string $locale): array 	{ 		$questions_data = []; 		$questions = Question::whereIn('id', $questions_answers->pluck('id'))->with(['category', 'answers.question'])->get();  		foreach ($questions_answers as $key => $question_answer) { 			$question = $questions->where('id', $question_answer->getKey())->first();  			array_push($questions_data, [ 				'id' => $question_answer->getKey(), 				'title' => $question->getTranslation('title', $locale), 				'code' => $question->code, 				'comments' => $question->answers->where('comment', '<>', '')->where('client_survey_id', $client_survey->getKey())->pluck('comment'), 				'category_name' => $question?->category ? $question->category->getTranslation('name', $locale) : __('questions.classes_list.' . QuestionClass::Demographic->value), 				'answers' => $question->answers->where('client_survey_id', $client_survey->getKey()), 				'score' => $question_answer['answer_value_sum'], 			]); 		}  		return $questions_data; 	}  	public static function getExtraAndDemographicQuestionsNamesForSelectedFilters(ClientSurvey $client_survey, array $filter_values, string $locale): Collection 	{ 		$filter_questions = $client_survey->extraQuestions->whereIn('id', array_keys($filter_values))->merge($client_survey->demographicQuestions->whereIn('id', array_keys($filter_values)));  		$filter_collection = new Collection;  		$standard_answers_array = [ 			__('pdf.printable-survey.completely-disagree', [], $locale), 			__('pdf.printable-survey.mostly-disagree', [], $locale), 			__('pdf.printable-survey.mostly-agree', [], $locale), 			__('pdf.printable-survey.completely-disagree', [], $locale), 		];  		foreach ($filter_questions as $filter_question) { 			$filter_values_single = array_flip(array_map(function ($value) { return $value - 1; }, $filter_values[$filter_question->getKey()] ?? []));  			$filter_collection->push([$filter_question->getDefaultTranslation('title'), array_intersect_key(!$filter_question->available_answers ? $standard_answers_array : $filter_question->available_answers, $filter_values_single)]); 		}  		return $filter_collection; 	}  	public static function getLocationNames(array $locations_ids, string $locale): array 	{ 		$location_names = [];  		$locations = Location::whereIn('id', $locations_ids)->get();  		$locations->each(function (Location $location) use (&$location_names, &$locale) { 			array_push($location_names, $location->getTranslation('title', $locale)); 		});  		return $location_names; 	}  	public static function getDepartmentNames(array $department_ids, string $locale): array 	{ 		$department_names = [];  		$departments = Department::whereIn('id', $department_ids)->get();  		$departments->each(function (Department $department) use (&$department_names, &$locale) { 			array_push($department_names, $department->getTranslation('title', $locale)); 		});  		return $department_names; 	}  	public static function getExtraQuestionsWithAverageScore(ClientSurvey $client_survey, string $locale, array $questin_types): array 	{ 		$extra_questions = []; 		$extra_questions_with_answers = $client_survey->load(['extraQuestions.answers.question'])->extraQuestions->whereIn('type', array_column($questin_types, 'value'));  		foreach ($extra_questions_with_answers as $question) { 			array_push($extra_questions, [ 				'id' => $question->getKey(), 				'type' => $question->type, 				'title' => $question->getTranslation('title', $locale), 				'comments' => array_filter($question->answers->pluck('comment')->toArray()), 				'answers' => $question->answers, 				'score' => ExportExcelService::extraQuestionCalculateAvarage($question), 			]); 		}  		return $extra_questions; 	}  	public static function extraQuestionCalculateAvarage(Question $question): float 	{ 		$question_answers_count = $question->answers()->count();  		switch ($question->type) { 			case QuestionType::Checkbox->value: 				return $question_answers_count > 0 ? array_sum($question->answers->map(function ($item) { 					return $item->getRawOriginal('answer_value'); 				})->toArray()) / $question_answers_count : 0;  			case QuestionType::StandardRadio->value: 				return $question_answers_count > 0 ? array_sum($question->answers->map(function ($item) { 					return $item->getRawOriginal('answer_value'); 				})->toArray()) / $question_answers_count : 0;  			case QuestionType::CustomRadio->value: 				return $question_answers_count > 0 ? array_sum($question->answers->map(function ($item) { 					return $item->getRawOriginal('answer_value'); 				})->toArray()) / $question_answers_count : 0;  			default: 				return 0; 		} 	}  	public function exportBasicReportExcel(): BinaryFileResponse{}  	public function exportBasicReportStatisticExcel(): BinaryFileResponse{}  	public function getEsiDataForClinetSurvey(): EsiDataObject{}  	public function getAnswersByDepartment(array $applicant_ids): array{}  	public function getExtraTextualQuestions(): array{} }