<?php

namespace Modules\Internal\Services;

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

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

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

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

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

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

    public function handlePlanRow(Corporate $corporate, $row)
    {
        try {
            $plan_data = $row;
            $plan_data["corporate_id"] = $corporate->id;
            $this->validatePlanRow($plan_data);
            $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)
    {
        if (empty($row['service_code'])) {
            throw new ImportRowException(__('benefit.SERVICE_CODE_REQUIRED'), 0, null, $row);
        }
        if (empty($row['plan_code'])) {
            throw new ImportRowException(__('benefit.PLAN_CODE_REQUIRED'), 0, null, $row);
        }
        if (empty($row['code'])) {
            throw new ImportRowException(__('benefit.BENEFIT_CODE_REQUIRED'), 0, null, $row);
        }
        if (empty($row['corporate_benefit_code'])) {
            throw new ImportRowException(__('benefit.CUSTOMER_BENEFIT_CODE_REQUIRED'), 0, null, $row);
        }
        if (empty($row['description'])) {
            throw new ImportRowException(__('benefit.DESCRIPTION_REQUIRED'), 0, null, $row);
        }
        if (empty($row['limit_amount'])) {
            throw new ImportRowException(__('benefit.LIMIT_AMOUNT_REQUIRED'), 0, null, $row);
        }
        if (empty($row['msc'])) {
            throw new ImportRowException(__('benefit.MSC_REQUIRED'), 0, null, $row);
        }
        if (empty($row['genders'])) {
            throw new ImportRowException(__('benefit.GENDER_REQUIRED'), 0, null, $row);
        }
    }

    public function handleBenefitRow(Corporate $corporate, $row)
    {

        try {
            $benefit_data = $row;
            $benefit_data["corporate_id"] = $corporate->id;
            $this->validateBenefitRow($benefit_data);


            $plan = $corporate->plans()
                            ->where('corporate_plan_id', $benefit_data['plan_code'])
                            ->first();

            // $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,
                'active' => 1,
                'description' => $benefit_data['description'],
            ]);

            $corporateBenefit = $corporate->corporateBenefits()->updateOrCreate([
                'benefit_id' => $benefit->id,
                'plan_id' => $plan->id
            ], $benefit_data);

            return $corporateBenefit;
        } catch (\Exception $e) {
            // dd($e->getMessage());
            throw $e;
        }
    }
}
