<?php

namespace App\Exports\MME_Export\Prevention;

use Carbon\Carbon;
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;
use PhpOffice\PhpSpreadsheet\Shared\Date;

class PreventionExport implements FromCollection, WithMapping, WithHeadings, WithChunkReading, WithColumnFormatting, WithColumnWidths
{
	private $prevention_records, $testName;

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

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

	private function excelDate($value)
	{
		if ($value === null || $value === '') {
			return null;
		}

		if (is_numeric($value)) {
			return $value;
		}

		try {
			$carbonDate = Carbon::createFromFormat('Y-m-d', (string) $value);
		} catch (\Throwable $e) {
			try {
				$carbonDate = Carbon::parse($value);
			} catch (\Throwable $ignored) {
				return $value;
			}
		}

		return Date::dateTimeToExcel($carbonDate->startOfDay());
	}

	public function map($row): array
	{
		if ($this->testName == "LogSheet") {
			return [
				$row->getConnectionName(),
				$row['He Code'],
				$row['Clinic Code'],
				$row['Reg year'],

				$row['Pid'],
				$row['FuchiaID'],
				$row['PrEPCode'],


				$row['Reg_Date'],

				$row['Visit_Date'],


				$row['Register Agey'],
				$row['Register Agem'],
				$row['Current Agey'],
				$row['Current Agem'],
				$row['Sex'],
				$row['Initial Risk'],
				$row['Risk changed'],
				$row['Risk changed Date'],
				$row['Main_Risk'],
				$row['Sub_Risk'],

				$row['New_Old'],
				$row['Substantial Risk'],
				$row['Meeting Point'],
				$row['Service Provision1'],
				$row['Service Provision2'],
				$row['Service Provision3'],
				$row['HE_Section'],
				$row['Ns_distribute'],
				$row['Condom_m'],
				$row['Condom_f'],
				$row['Ns_return'],
				$row['HIV Status'],

				$row['Test_duration'],
				$row['HTS done'],
				$row['HIV_Final_result'],
				$row['date_confirm'],
				$row['Reach_whom'],

				$row['Source_doc'],
				$row['Mental_Health'],
				$row['PHQ4_Q1_Q2'],
				$row['PHQ4_Q3_Q4'],
				$row['OST_Done'],
				$row['OST_Accept'],

				$row['Decline_Reason'],
				$row['OST_Initial_Date'],
				$row["OST_Eligible"],
				$row["Decline_Reason_new"],
				$row["Referral_Coupon"],
				$row['Test_Clinic'],
				$row['Test_New_Old'],
					$row['Remark'],
					$row['Mh_remark'],
					$row['Du_remark'],
					$row['Brief_Intervention'],
					$row['MMT_Code'],
					$row['du_Explain_harm_act'] ?? null,
					$row['du_Introduce_services'] ?? null,
					$row['du_Discuss_redue_risk'] ?? null,
					$row['du_Explain_OST_ref'] ?? null,
					$row['Reached_township'],
					
					$row['updated_by'],
					$row['du_Shared_NS'] ?? null,
					$row['du_Shared_waterforinj'] ?? null,
					$row['du_Othersite_inj'] ?? null,
					$row['du_Cxofinj'] ?? null,
					$row['du_Polydruguse'] ?? null,
					$row['du_Overdose'] ?? null,
					$row['du_Increase_freq_dosage'] ?? null,
					$row['du_Work_family_impact'] ?? null,
					$row['du_Other_impact'] ?? null,
					$row['du_Criticised'] ?? null,
					$row['du_Detoxification'] ?? null,
					$row['du_Problematic_DU'] ?? null,
					$row['du_BI_done'] ?? null,
					$this->excelDate($row['mtd_date'] ?? null),

			];
		} else if ($this->testName == "CBS") {
			return [
				$row->getConnectionName(),
				$row['He Code'],
				$row['Clinic Code'],
				$row['Reg year'],

				$row['Pid'],
				$row['FuchiaID'],
				$row['PrEPCode'],
				$row['Visit_Date'],

				$row['Register Agey'],
				$row['Register Agem'],

				$row['Current Agey'],
				$row['Current Agem'],

				$row['Sex'],
				$row['Main_Risk'],
				$row['Sub_Risk'],

				$row['New_Old'],
				$row['Meeting Point'],
				$row['Service Provision'],
				$row['Retesting'],
				$row['HIV_determine_result'],
				$row['HIV result'],
				$row['Counselling_pretest'],
				$row['Counselling_posttest'],
				$row['Refer_to'],
				$row['date_confirm'],
				$row['HIV Sero-Status'],
				$row['Remark'],
			];
		} elseif ($this->testName == "Server_Confidential") {
			return [
				$row->getConnectionName(),
				$row['He_code'],
				$row['Clinic Code'],
				$row['Reg year'],

				$row['Pid'],
				$row['FuchiaID'],
				$row['PrEPCode'],
				$row['Eyes_code'],
				$row['Kc'],
				$row['Reg Date'],


				$row['Date of Birth'],
				$row['Register Agey'],
				$row['Register Agem'],
				$row['Current Agey'],
				$row['Current Agem'],


				$row['Gender'],
				$row['Former Risk'],
				$row['Risk changed'],
				$row['Risk Change_Date'],

				$row['Main Risk'],
				$row['Sub Risk'],
				$row['created_at'],
				$row['updated_at'],
				
				

			];
		}
	}
	public function headings(): array
	{
		if ($this->testName == "LogSheet") {
			return [
				'Database',
				'He Code',
				'Clinic Code',
				'Register Year',
				'Pid',
				'Fuchia ID',
				'PrEP Code',

				'Clinic Register Date',
				'Visit Date',
				'Register Agey',
				'Register Age month',
				'Current Age(y)',
				'Current Age(m)',
				'Sex',
				'Initial Risk',
				'Risk changed',
				'Risk changed Date',
				'Main Risk(Current)',
				'Sub Risk',

				'Reach New_Old',
				'Substantial Risk',
				'Meeting Point',
				'Service Provision1',
				'Service Provision2',
				'Service Provision3',
				'HE_Section',
				'Ns_distribute',
				'Condom_m',
				'Condom_f',
				'Ns_return',
				'HIV Status',
				'Test_duration',
				'HTS done',
				'HIV_Final_result',
				'date_confirm',
				'Reach_whom',
				'Source_doc',
				'Mental_Health',
				'PHQ4_Q1_Q2',
				'PHQ4_Q3_Q4',
				'OST_Done',
				'OST_Accept',
				'Decline_Reason',
				'OST_Initial_Date',
				"OST_Eligible",
				'Decline_Reason_new	',
				'Referral_Coupon',
				'Test_Clinic',
				'Test_New_Old',
					'Remark',
					'Mental Health',
				'Drug Use',
				'Brief Intervention',
				'MMT Code',
				'Explain Harm Activity',
				'Introduce Services',
				'Discuss Reduce Risk',
				'Explain OST Referral',
				'Reached Township',
				
				'updated_by',
				'Shared_NS',
				'Shared_waterforinj',
				'Othersite_inj',
				'Cxofinj',
				'Polydruguse',
				'Overdose',
				'Increase_freq_dosage',
				'Work_family_impact',
				'Other_impact',
				'Criticised',
				'Detoxification',
				'Problematic_DU',
				'BI_done',
				'mtd_date',
				
			];
		} else if ($this->testName == "CBS") {
			return [
				'Database',
				'He Code',
				'Clinic Code',
				'Register Year',
				'Pid',
				'FuchiaID',
				'PrEPCode',
				'Visit_Date',

				'Register Agey',
				'Register Age(m)',

				'Current Age(y)',
				'Current Age(m)',

				'Sex',
				'Main_Risk',
				'Sub_Risk',

				'New_Old',
				'Meeting Point',
				'Service Provision',
				'Retesting',
				'HIV_determine_result',
				'HIV Sero-Status',
				'Counselling_pretest',
				'Counselling_posttest',
				'Refer_to',
				'date_confirm',
				'HIV Sero-Status',
				'Remark',
			];
		} elseif ($this->testName == "Server_Confidential") {
			return [
				'Database',
				'He Code',
				'Clinic Code',
				'Register Year',
				'Pid',
				'Fuchia ID',
				'PrEP Code',
				'Eyes_code',
				'Kc',
				'Register Date',
				'Calculated DOB',
				'Register Agey',
				'Register Age month',
				'Current Age(y)',
				'Current Age(m)',
				'Sex',
				'Initial Risk',
				'Risk changed',
				'Risk changed Date',
				'Main Risk(Current Risk)',
				'Sub Risk',
				'Created_at',
				'Updated_at',
				
				

			];
		}
	}
	public function chunkSize(): int
	{
		return config('mme_export.chunk_size', 3000);
	}
	public function columnFormats(): array
	{
		if ($this->testName == "LogSheet") {
			return [
				'H' => 'dd-mm-yyyy',
				'I' => 'dd-mm-yyyy',
				'Q' => 'dd-mm-yyyy',
				'AR' => 'dd-mm-yyyy',
				'AI' => 'dd-mm-yyyy',
				'BZ' => 'dd-mm-yyyy',
			];
		} else if ($this->testName == "CBS") {
			return [
				'H' => 'dd-mm-yyyy',
				'W' => 'dd-mm-yyyy',
			];
		} elseif ($this->testName == "Server_Confidential") {
			return [
				'I' => 'dd-mm-yyyy',
				'J' => 'dd-mm-yyyy',
			];
		}
	}

	public function columnWidths(): array
	{
		$columns = [];
		foreach (range('A', 'Z') as $char) {
			$columns[] = $char;
		}
		foreach (range('A', 'Z') as $char1) {
			foreach (range('A', 'Z') as $char2) {
				$columns[] = $char1 . $char2;
			}
		}
		return array_fill_keys($columns, 15);
	}
}
