<?php

namespace Modules\Internal\Services;

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

class CorporateService
{
            /*
            Refer to Coverage code :
            MEDIVAC = Medical evacuation
            MSO = Medical second opinion
            PAC = Personal Assistant Concierge
            OPDE = Outpatient Dental
            IP=Inpatient
            OP=Outpatient
            MA=Maternity
            ANC = Ante/Post Natal Care
            DE=Dental
            GL=Glasses
            SP=Special Treatment
            PF=Pooled Fund
            MCU=Medical Check Up
            KB-VACC = Family Planning/Vaccination
            LAB = Test Diagnostic
            PHAR = Pharmacy
        **/
    protected $service_code = ['MEDIVAC', 'MSO', 'PAC', 'OPDE', 'IP', 'OP', 'MA', 'ANC', 'DE', 'GL', 'SP', 'PF', 'MCU', 'KB-VACC', 'LAB', 'PHAR'];

    protected function corporatePlansId($corporate_id){
        $plans = Plan::where('corporate_id', $corporate_id)->get()->toArray();
        $current_corporate_plans_id = [];
        if($plans){
            foreach($plans as $plan){
                array_push($current_corporate_plans_id,$plan['corporate_plan_id']);
            }
        }
        return  $current_corporate_plans_id;
    }

    protected function validatePlanRow($row, $corporate_id)
    {
        // $corporate_policy = CorporatePolicy::where('corporate_id', $corporateId)->get();
        $plans = Plan::where('corporate_id', $corporate_id)->get()->toArray();
        $current_corporate_plans = $this->corporatePlansId($corporate_id);

        $plan_code = [1, 2, 3, 4];
        $prorate_type = [0, 1, 2];
        $family_plan = ['F', 'S', 'N'];

        if (empty($row['service_code'])) {
            throw new ImportRowException(__('plan.REQUIRED', [
                'attribute' => 'Service Code'
            ] ), 0, null, $row);
        } else if (!in_array($row['service_code'], $this->service_code)){
            throw new ImportRowException(__('plan.NOT_MATCH', [
                'attribute' => 'Service Code',
                'code' => $row['service_code']
            ]), 0, null, $row);
        }

        if (empty($row['corporate_plan_id'])) {
            throw new ImportRowException(__('plan.REQUIRED', [
                'attribute' => 'Plan'
            ]), 0, null, $row);
        } else if(!in_array($row['corporate_plan_id'], $current_corporate_plans)){
            // throw new ImportRowException(__('plan.NOT_MATCH', [
            //     'attribute' => 'Plans',
            //     'code' => $row['corporate_plan_id']
            // ]), 0, null, $row);
        }

        if (empty($row['code'])) {
            throw new ImportRowException(__('plan.REQUIRED', [
                'attribute' => 'Customer Plan'
            ]), 0, null, $row);
        }

        if (!in_array($row['type'], $plan_code)){
            throw new ImportRowException(__('plan.NOT_MATCH', [
                'attribute' => 'Plan Type',
                'code' => $row['type']
            ]), 0, null, $row);
        } else if ($row['type'] == 2 && $row['limit_rules'] != 999999999){
            throw new ImportRowException(__('plan.LIMIT_PLAN'), 0, null, $row);
        }

        // if (!empty($row['start'])) {
        //     throw new ImportRowException(__('plan.REQUIRED', [
        //         'attribute' => 'Start Date of Plan'
        //     ]), 0, null, $row);
        // }

        // if (empty($row['type'])) {
        //     throw new ImportRowException(__('plan.TYPE_REQUIRED'), 0, null, $row);
        // } else if (!in_array($row['type'],$plan_code)){
        //     throw new ImportRowException(__('plan.NOT_MATCH', [
        //         'attribute' => 'Plan Type',
        //         'code' => $row['code']
        //     ]), 0, null, $row);
        // }

        if (!empty($row['family_plan']) && !in_array($row['family_plan'],$family_plan)) {
            throw new ImportRowException(__('plan.NOT_MATCH', [
                'attribute' => 'Family Plan',
                'code' => $row['family_plan']
            ]), 0, null, $row);
        }

        if (!empty($row['prorate_type']) && !in_array($row['prorate_type'], $prorate_type)) {
            throw new ImportRowException(__('plan.NOT_MATCH', [
                'attribute' => 'Prorate Type',
                'code' => $row['prorate_type']
            ]), 0, null, $row);
        }

        if (!empty($row['prorate_lookup']) && !in_array($row['prorate_lookup'], $prorate_type)) {
            throw new ImportRowException(__('plan.NOT_MATCH', [
                'attribute' => 'Prorate Lookup',
                'code' => $row['prorate_lookup']
            ]), 0, null, $row);
        }

        if (empty($row['limit_rules'])) {
            throw new ImportRowException(__('plan.PLAN_LIMIT_REQUIRED'), 0, null, $row);
        } else {

        }

        if (empty($row['msc'])) {
            throw new ImportRowException(__('plan.MSC_REQUIRED'), 0, null, $row);
        }
    }


    public function handlePlanRow(Corporate $corporate, $row)
    {
        try {
            $plan_data = $row;
            $this->validatePlanRow($plan_data, $corporate->id);
            $plan_data["corporate_id"] = $corporate->id;
            $plan = $corporate->plans()->updateOrCreate([
                'corporate_plan_id' => $plan_data['corporate_plan_id'],
                // 'active' => 0,
            ], $plan_data);

            return $plan;
        } catch (\Exception $e) {
            throw $e;
        }
    }

    protected function validateBenefitRow($row, $corporate_id)
    {
        $max_frequence = 7;
        $budget_aso = [1,2];
        $current_corporate_plans = $this->corporatePlansId($corporate_id);
        if (empty($row['service_code'])) {
            throw new ImportRowException(__('plan.REQUIRED', [
                'attribute' => 'Service Code'
            ]), 0, null, $row);
        } else if (!in_array($row['service_code'], $this->service_code)){
            throw new ImportRowException(__('plan.NOT_MATCH', [
                'attribute' => 'Service Code',
                'code' => $row['service_code']
            ]), 0, null, $row);
        }

        if (empty($row['plan_code'])) {
            throw new ImportRowException(__('benefit.PLAN_CODE_REQUIRED'), 0, null, $row);
        } else if (!in_array($row['plan_code'], $current_corporate_plans)){
            // throw new ImportRowException(__('plan.NOT_MATCH', [
            //     'attribute' => 'Plan',
            //     'code' => $row['plan_code']
            // ]), 0, null, $row);
        }



        if (empty($row['code'])) {
            throw new ImportRowException(__('plan.REQUIRED', [
                'attribute' => 'Benefit Code'
            ]), 0, null, $row);
        }

        if (empty($row['corporate_benefit_code'])) {
            throw new ImportRowException(__('Customer Benefit Code Required', [
                'attribute' => 'Customer Benefit Code'
            ]), 0, null, $row);
        }


        if (empty($row['description'])) {
            throw new ImportRowException(__('plan.REQUIRED', [
                'attribute' => 'Description'
            ]), 0, null, $row);
        }
        if (empty($row['limit_amount'])) {
            throw new ImportRowException(__('plan.REQUIRED', [
                'attribute' => 'Limit Amount'
            ]), 0, null, $row);
        }
        if (empty($row['msc'])) {
            throw new ImportRowException(__('plan.REQUIRED', [
                'attribute' => 'MSC'
            ]), 0, null, $row);
        }
        if (empty($row['genders'])) {
            throw new ImportRowException(__('plan.REQUIRED', [
                'attribute' => 'Gender'
            ]), 0, null, $row);
        }
        if (!empty($row['max_frequency_period']) && $row['max_frequency_period'] >= $max_frequence){ // jenis frequence
            throw new ImportRowException(__('plan.MAX_FREQUENCY'), 0, null, $row);
        }



        if (!empty($row['budget']) && !in_array($budget_aso)) {
            throw new ImportRowException(__('plan.MAX_FREQUENCY'), 0, null, $row);
        }


        // if (empty($row['limit_free_tc'])){
        //     throw new ImportRowException(__('plan.REQUIRED', [
        //         'attribute' => 'Limit Free TC'
        //     ]), 0, null, $row);
        // }
    }

    public function handleBenefitRow(Corporate $corporate, $row)
    {
        try {
            $row['limit_free_tc'] = 0;
            $benefit_data = $row;
            $this->validateBenefitRow($benefit_data, $corporate->id);
            $benefit_data["corporate_id"] = $corporate->id;
            $plan = $corporate->plans()
                            ->where('corporate_plan_id', $benefit_data['plan_code'])
                            ->first();
            $benefit_data['plan_code'] = $plan->id;
            $benefit = Benefit::updateOrCreate([
                'code' => $benefit_data['code'],
                'service_code' => $plan->service_code,
            ], [
                'code' => $benefit_data['code'],
                'service_code' => $plan->service_code,
                'description' => $benefit_data['description'],
            ]);
            $corporateBenefit = $corporate->corporateBenefits()->updateOrCreate([
                'benefit_id' => $benefit->id,
                'plan_id' => $plan->id,
                'corporate_id' => $corporate->id,
            ], $benefit_data);

            return $corporateBenefit;
        } catch (\Exception $e) {
            throw $e;
        }
    }
}
