<?php

namespace Modules\Internal\Services;

use App\Exceptions\ImportRowException;
use App\Models\Benefit;
use App\Models\Corporate;
use App\Models\Icd;
use App\Models\Exclusion;
use App\Models\Plan;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;

class ExclusionService
{
    protected function validateDiagnosisExclusionRow($row)
    {
        if (empty($row['service_code'])) {
            throw new ImportRowException(__('plan.REQUIRED'), 0, null, $row);
        }
    }

    public function handleDiagnosisExclusionRow(Corporate $corporate, $row)
    {
        try {
            if (!empty($row['ip_exclusion'])) {
                $excl_array = explode('|', $row['ip_exclusion']);
                if ($excl_array[0] == '3') {
                    $icd = Icd::where('code', $row['code'])->first();
                    $exclusion = $icd->exclusions()->create([
                        'corporate_id' => $corporate->id,
                        'service_code' => 'IP',
                        'type' => 'diagnosis'
                    ]);

                    if (!empty($excl_array[1])) { //msc
                        $msc = explode(',', $excl_array[1]);
                        collect($msc)->each(function ($m) use ($exclusion) {
                            $exclusion->rules()->create([
                                'name' => 'msc',
                                'values' => $m
                            ]);
                        });
                    }
                    if (!empty($excl_array[2])) { //genders
                        $genders = explode(',', $excl_array[2]);
                        collect($genders)->each(function ($gender) use ($exclusion) {
                            if ($gender == 'M') {
                                $exclusion->rules()->create([
                                    'name' => 'gender',
                                    'values' => 'male'
                                ]);
                            } else if ($gender == 'F') {
                                $exclusion->rules()->create([
                                    'name' => 'gender',
                                    'values' => 'female'
                                ]);
                            }
                        });
                    }
                    if (!empty($excl_array[3])) { //min_age
                        $exclusion->rules()->create([
                            'name' => 'min_age',
                            'values' => $excl_array[3]
                        ]);
                    }
                    if (!empty($excl_array[4])) { //max_age
                        $exclusion->rules()->create([
                            'name' => 'max_age',
                            'values' => $excl_array[4]
                        ]);
                    }
                    if (!empty($excl_array[5])) { //plans
                        $exclusion->rules()->create([
                            'name' => 'plan',
                            'values' => $excl_array[5]
                        ]);
                    }
                }
            }

            if (!empty($row['op_exclusion'])) {
                $excl_array = explode('|', $row['op_exclusion']);
                if ($excl_array[0] == '3') {
                    $icd = Icd::where('code', $row['code'])->first();
                    if (!$icd) {
                        throw new ImportRowException(__('icd.NOT_FOUND'), 0, NULL, $row);
                    }
                    $exclusion = $icd->exclusions()->create([
                        'corporate_id' => $corporate->id,
                        'service_code' => 'OP',
                        'type' => 'diagnosis',
                        'active' => 1
                    ]);

                    if (!empty($excl_array[1])) { //msc
                        $msc = explode(',', $excl_array[1]);
                        collect($msc)->each(function ($m) use ($exclusion) {
                            $exclusion->rules()->create([
                                'name' => 'msc',
                                'values' => $m
                            ]);
                        });
                    }
                    if (!empty($excl_array[2])) { //genders
                        $genders = explode(',', $excl_array[2]);
                        collect($genders)->each(function ($gender) use ($exclusion) {
                            if ($gender == 'M') {
                                $exclusion->rules()->create([
                                    'name' => 'gender',
                                    'values' => 'male'
                                ]);
                            } else if ($gender == 'F') {
                                $exclusion->rules()->create([
                                    'name' => 'gender',
                                    'values' => 'female'
                                ]);
                            }
                        });
                    }
                    if (!empty($excl_array[3])) { //min_age
                        $exclusion->rules()->create([
                            'name' => 'min_age',
                            'values' => $excl_array[3]
                        ]);
                    }
                    if (!empty($excl_array[4])) { //max_age
                        $exclusion->rules()->create([
                            'name' => 'max_age',
                            'values' => $excl_array[4]
                        ]);
                    }
                    if (!empty($excl_array[5])) { //plans
                        $codePlan = explode(',', $excl_array[5]);
                        collect($codePlan)->each(function ($codePlan) use ($corporate, $exclusion) {
                            $isCodeplan = Plan::where(['code' => $codePlan, 'corporate_id' => $corporate->id])->first();
                            if (!$isCodeplan) {
                                throw new ImportRowException(__('codePlan.NOT_FOUND'), 0, NULL, $codePlan);
                            }
                            
                            $exclusion->rules()->create([
                                'name' => 'plan',
                                'values' => $codePlan
                            ]);
                        });
                    }
                } else if (!$excl_array[0]){
                    $icd = Icd::where(['code' => $row['code']])->first();
                    if (!$icd) {
                        throw new ImportRowException(__('icd.NOT_FOUND'), 0, NULL, $row);
                    }
                  // Cari entitas Exclusion yang sesuai dengan kriteria tertentu
                    $exclusion = Exclusion::where([
                        'corporate_id' => $corporate->id,
                        'exclusionable_id' => $icd->id,
                    ])->first();

                    // Jika entitas ditemukan, perbarui nilai 'active' menjadi 0
                    if ($exclusion) {
                        $exclusion->update(['active' => 0]);
                    }

                }
            }

            if (!empty($row['de_exclusion'])) {
                $excl_array = explode('|', $row['de_exclusion']);
                if ($excl_array[0] == '3') {
                    $icd = Icd::where('code', $row['code'])->first();
                    $exclusion = $icd->exclusions()->create([
                        'corporate_id' => $corporate->id,
                        'service_code' => 'DE',
                        'type' => 'diagnosis'
                    ]);

                    if (!empty($excl_array[1])) { //msc
                        $msc = explode(',', $excl_array[1]);
                        collect($msc)->each(function ($m) use ($exclusion) {
                            $exclusion->rules()->create([
                                'name' => 'msc',
                                'values' => $m
                            ]);
                        });
                    }
                    if (!empty($excl_array[2])) { //genders
                        $genders = explode(',', $excl_array[2]);
                        collect($genders)->each(function ($gender) use ($exclusion) {
                            if ($gender == 'M') {
                                $exclusion->rules()->create([
                                    'name' => 'gender',
                                    'values' => 'male'
                                ]);
                            } else if ($gender == 'F') {
                                $exclusion->rules()->create([
                                    'name' => 'gender',
                                    'values' => 'female'
                                ]);
                            }
                        });
                    }
                    if (!empty($excl_array[3])) { //min_age
                        $exclusion->rules()->create([
                            'name' => 'min_age',
                            'values' => $excl_array[3]
                        ]);
                    }
                    if (!empty($excl_array[4])) { //max_age
                        $exclusion->rules()->create([
                            'name' => 'max_age',
                            'values' => $excl_array[4]
                        ]);
                    }
                    if (!empty($excl_array[5])) { //plans
                        $exclusion->rules()->create([
                            'name' => 'plan',
                            'values' => $excl_array[5]
                        ]);
                    }
                }
            }

            if (!empty($row['ma_exclusion'])) {
                $excl_array = explode('|', $row['ma_exclusion']);
                if ($excl_array[0]) {
                }
            }

            if (!empty($row['sp_exclusion'])) {
                $excl_array = explode('|', $row['sp_exclusion']);
                dd($excl_array);
                if ($excl_array[0]) {
                }
            }

            if (!empty($row['pre_exist_exclusion'])) {
                $excl_array = explode('|', $row['pre_exist_exclusion']);
                dd($excl_array);
                if ($excl_array[0]) {
                }
            }

            if (!empty($row['op_de_exclusion'])) {
                $excl_array = explode('|', $row['op_de_exclusion']);
                dd($excl_array);
                if ($excl_array[0]) {
                }
            }

            if (!empty($row['maternity_waiting'])) {
                $excl_array = explode('|', $row['maternity_waiting']);
                dd($excl_array);
                if ($excl_array[0]) {
                }
            }
            return true;
        } catch (\Exception $e) {
            throw $e;
        }
    }
}
