<?php

namespace App\Exports\MME_Export\MentalHealth;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;

class MentalExport implements FromCollection, WithMapping, WithHeadings, WithChunkReading, WithColumnFormatting, WithColumnWidths
{
  private $mental_records, $testName;

  public function __construct($mental_records, $testName)
  {
    $this->mental_records = $mental_records;
    $this->testName = $testName;
  }

  public function collection()
  {
    return $this->mental_records;
  }

  public function map($row): array
  {
    if ($this->testName == "Screening") {
      return [				
	$row->getConnectionName(),
        $row['He Code'],
        $row['Clinic Code'],
      $row["Counselling_Date"],
      $row['Pid'],
      $row['FuchiaID'],
      $row['Gender'],
      $row['Reg year'],
      $row["Register Agey"],
      $row['Register Agem'],
      $row['Current Agey'],
      $row['Current Agem'],
      $row['Main_Risk'],
      $row['Sub_Risk'],
      $row['Final_Result'],
      $row['Ref_cf'],
      $row['Basic_assessment'],
      $row['Basic_mh_assessment'],
      $row['Q1_Q2'],
      $row['Q3_Q4'],
      $row['gad7_check'],
      $row['gad7_newfup'],
      $row['gad7_amount'],
      $row['phq9_check'],
      $row['phq9_newfup'],
      $row['PHQ9_amount'],
      
      $row['Drug3M'],
      $row['Sexdrug'],
      $row['ChemSex'],
       $row['A'],
      $row['B'],
      $row['C'],
      $row['D'],
      $row['E'],
      $row['Remark'],
      ];
    }
    else if($this->testName == "Register"){
    	return[
    	$row->getConnectionName(),
        $row["Pid"],
        $row["FuchiaID"],
        $row["PrePCode"],
        $row["Current Agey"],
        $row["Gender"],
        $row["Final_Result"],
        $row["Reg_date"],
        $row["Main_Risk"],
        $row["If_pwud"],
        $row["If_pwudEx"],
        //$row["Alcohol_drinking"],

        $row["PHQ4_Q1Q2"],
        $row["PHQ4_Q3Q4"],
        $row["GAD7_Score"],
        $row["PHQ9_Score"],

        $row["Psychosis"],
        $row["Delusion"],
        $row["Illusion"],// This is Marked behavioral change
        $row["Hallucination"],

        $row["Psy_others"],
        $row["Duration"],
        $row["Suicidal_risk"],
        $row["Sucidal_yes"],
        $row["Drug_uses3month"],
        $row["Drug_willingness"],
        $row["Sexual_drug"],
        $row["SexualDrug_willigness"],
        $row["Injectable"],
        $row["Injectable_yes"], //TimeFrame
        $row["ASSIST_score"],
        $row["Drug_name_1"],
        $row["Drug_name_1_risk"],
        $row["Drug_name_2"],
        $row["Drug_name_2_risk"],
        $row["Drug_name_3"],
        $row["Drug_name_3_risk"],
        $row["Drug_name_4"],
        $row["Drug_name_4_risk"],
        $row["Drug_name_5"],
        $row["Drug_name_5_risk"],
        $row["Brief"],
        $row["Brief_plan"],
        $row["Brief_stage"],
        $row["Brief_no"],
        $row["Psychosocial_mam"],
        $row["Pharmacologica_mam"],
        $row["Fluoxetine"],
        $row["Risperidone"],
        $row["Tre_other"],
        $row["Refer_psychiatrist"],
        $row["Depression_diag"],
        $row["Anxiety_diag"],
        $row["Psychosis_diag"],
        $row["Other_diag"],
        $row["MD_initial"],
        $row["CSL_initial"],
        $row["Next_meetdate"],

        ];
    }
    else if($this->testName == "Followup"){
    	return [
    	$row->getConnectionName(),
        $row["Visit_date"],
        $row["Pid"],
        $row["FuchiaID"],
        $row["PrePCode"],
        $row["Current Agey"],
        $row["Gender"],
        $row["Main_Risk"],
        $row["Final_Result"],
        $row["Improve_symp"],
        $row["Adh_problem"],
        $row["Mental_asses_rescreen"],
        $row["gad7_score_followup"],
        $row["phq9_score_followup"],
        $row["Problem_sex_drug"],// new var // 15

        //$row["No_asses_describe"],
        //$row["Drug_reassesment"],
        $row["Assist_score_screen"],
        $row["Drug_1"],
        $row["Scroe_1"],
        $row["Scroe_1_risk"],
        $row["Drug_2"],
        $row["Scroe_2"],
        $row["Scroe_2_risk"],
        $row["Drug_3"],
        $row["Scroe_3"],
        $row["Scroe_3_risk"],
        $row["Drug_4"],
        $row["Scroe_4"],
        $row["Scroe_4_risk"],
        $row["Drug_5"],
        $row["Scroe_5"],
        $row["Scroe_5_risk"],// 16

        $row["Brief"],
        $row["Brief_plan"],
        $row["Brief_stage"],
        $row["Useof_newdrug"],
        $row["Newdrug_name"],
        $row["Assist_scoring"],
        $row["Sucidal_risk_between_lastVist"],
        $row["MD_consultation"],
        $row["Phamological_effect"],//8
        //$row["Extrapyramidal_effect"],
        //$row["Other_effect"],
        //$row["Management_effect"],
        //$row["Artane"],
        //$row["Other_management"],
        $row["Continue_same_traeat_describe"],
        $row["Increase_dosage_describe"],
        $row["Reduce_dosage_describe"],
        $row["Tapering_drug_describe"],
        $row["Restart_drug_describe"],

        $row["Refer_psychiatrist"],
        $row["Stop_drug"],
        $row["Psy_interview_mam"],
        $row["Other_refer_psychiatrist"],//9

        $row["Depression"],
        $row["Anxiety"],
        $row["Psychosis"],
        $row["Other_symptoms"],
        $row["MD_initial"],
        $row["Md_nextFollowDate"],
        $row["CSL_initial"],
        $row["Csl_nextFollowDate"],
  	];
    }
    
  }
  
  
  
  
  
  
  
  public function headings(): array
  {
    if ($this->testName == "Screening") {
      return [
        'Database',
        'He Code',
        'Clinic Code',
      'Visit Date',
      'Pid',
      'FuchiaID',
      'Gender',
      'Reg Year',
      'Register Age',
      'Register Age(Month)',
      'Current Age',
      'Current Age(Month)',
      'Main Risk',
      'Sub Risk',
      
      'HIV status',
      'Referred by CF',
      'Basic assessment',
      'Basic MH assessment',
      'Q1-Q4',
      'Q5-Q7',
      'GAD7_Done',
      'GAD7_New/Old',
      'GAD7 score',
      'PHQ9_Done',
      'PHQ9_New/Old',
      'PHQ9 score',
      '1. Any type of drug use within 6 months',
      '2. Sexual activities under the drug effect',
      '3. Assessment for problematic chemsex (during last 1 month)',
      'A',
      'B',
      'C',
      'D',
      'E',
      'Remark'
      ];
    }
    else if($this->testName == "Register"){
    	return [
    	'Database',
        'General ID',
        'Fuchia ID',
        'PrEP ID',
        'Age',
        'Sex',
        'HIV status',
        'Registration date',
        'Risk factor',
        'if PWID',
        'OST',
        //'Alcohol drinking',
        'Q1 - Q4',
        'Q5 - Q7',
        'GAD7',
        'PHQ9',
        'Psychosis',
        'Delusion',
        'Marked behavioral change',//'Illusion',
        'Hallucination',
        'Others',
        'Duration',
        'Suicidal risk (Attempt or thought)',
        'If Yes,',
        '1.Drug uses within 6months ( any type)',
        'Willingness to change',
        '2.Sexual activities under the drug effect',
        'Willingness to change2',
        '3.Injectable drug use',
        'Timeframe',
        'ASSIST Score ( Screening)',
        'Name of drug1',
        'Risk',
        'Name of drug2',
        'Risk',
        'Name of drug3',
        'Risk',
        'Name of drug4',
        'Risk',
        'Name of drug5',
        'Risk',
        'Brief Intervention(BI)',
        'If yes,planned goal',
        'Stage of BI',
        'If no BI,why',
        'Psychosocial Intervention at MAM',
        'Pharmacological management at MAM',
        'Fluoxetine',
        'Risperidone',
        'Others2',
        'Refer to psychiatrists',
        'Depression',
        'Anxiety disorder',
        'Psychosis',
        'Other',
        "MD's initial",
        "CSL's initial",
        'Next follow up date',
    	];
    }
    else if($this->testName == "Followup"){
    	return[
    	'Database',
        'Visit Date',
        'General ID',
        'FUCHIA ID',
        'PrEP ID',
        'Age',
        'Gender',
        'Risk factor',
        'Hiv Result',
        'Improvement of symptoms(any)',
        'Adherence problem for mental health drugs',
        'Mental Health assessment rescreening',
        //'PHQ4(Q1/Q2)',
        //'PHQ4(Q3/Q4)',
        'GAD7',
        'PHQ9',
        'Problematic sexualized drug use',// 15
        //'No assessment rescreening',
        //'Drug use assessment',
        'If yes,ASSIST rescreening',
        'Name of drug1',
        'Score1',
        'Risk',
        'Name of drug 2',
        'Score2',
        'Risk2',
        'Name of drug 3',
        'Score3',
        'Risk3',
        'Name of drug 4',
        'Score4',
        'Risk4',
        'Name of drug 5',
        'Score5',
        'Risk5',//16
        
        'Brief intervention(BI)',
        'If yes BI,Any change in planned goal',
        'State of BI',
        'Useof_newdrug',
        'Newdrug_name',
        'Assist_scoring',
        'Suicidal Risk between the last visit and current visit',
        'MD Consultation',
        'Pharmacological side effects',//8
        //'Extrapyramidal side effect',
        //'Other side effect',
        //'Management side effect',
        //'Artane(Trihexyphenidyl)',
        //'Other Management',
        'Continue the same treatment',
        'Increased the dosage',
        'Reduced the dosage',
        'Tapering the drug',
        'Restart the drug',
        'Refer to psychiatrist',
        'Stopping the drug',
        'Psychosocial intervention at MAM',
        'Refer to psychiatrist',
        'Depression',
        'Anxiety',
        'Psychosis',
        'Other',
        "MD's initial",
        "Next Follow up Date by MD",
        "CSL's initial",
        "Next Follow up Date by CSL",//18
    	];
    
    }
    
  }
  public function chunkSize(): int
  {
    return 5000; // Process 1,000 rows at a time
  }
  public function columnFormats(): array
  {
    if ($this->testName == "Screening") {
      return ["D" => "d-m-yyyy"];
    }
    if ($this->testName == "Register") {
      return ["H" => "d-m-yyyy","BE" =>"d-m-yyyy",];
    }
    if ($this->testName == "Followup") {
      return ["B" => "d-m-yyyy","BC"=>"d-m-yyyy","BE"=>"d-m-yyyy"];
    }
  }

 
  public function columnWidths(): array
  {
    return array_fill_keys(range('A', 'Z'), 15);
  }
  
  
  
  
}
