<?php

namespace App\Services;

use App\Events\ClaimApproved;
use App\Events\ClaimRequested;
use App\Models\Claim;
use App\Models\RequestLog;
use App\Models\Organization;
use App\Helpers\Helper;
use App\Models\Icd;
use App\Models\Member;
use Carbon\Carbon;
use App\Exceptions\ImportRowException;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;


use DB;
use Str;

class RequestLogService{

    public static function storeRequestLog(
        $row = null, 
        $code, 
        $member, 
        $paymentType, 
        $serviceCode, 
        $submissionDate, 
        $status, $organization_id = null, 
        $source
    )
    {
        try {
            $organization = False;
            if($organization_id){
                $organization = Organization::where('id', $organization_id)->first();
                if (!$organization){
                    throw new ImportRowException(__('Code Provider Tidak ditemukan', [
                        'attribute' => 'provider_code',
                        'code' => $row['provider_code']
                    ]), 403, null, $row);
                }
            };

            DB::beginTransaction();

            $requestLogData = [
                'code' => $code,
                'member_id' => $member->id,
                'submission_date' => $submissionDate ? $submissionDate : now(),
                'status' => $status,
                'payment_type' => $paymentType,
                'service_code' => $serviceCode,
                'policy_id' => $member->currentPolicy->id ?? null,
                'organization_id' => $organization ? $organization->id : 0,
                'source' => $source,
            ];

            $requestLog = RequestLog::create($requestLogData);

            DB::commit();

            return $requestLog;
        } catch (\Exception $error) {
            DB::rollBack();

            throw new \Exception($error);
        }
    }

    public static function storeClaimManagement($row, $member, $claim_request_id){
        try {
            $organization = 0;
            if($row['provider_code']){
                $organization = Organization::where('code', $row['provider_code'])->first();
                if (!$organization){
                    throw new ImportRowException(__('Provider Tidak ditemukan'), 0, null, $row);
                }
            };
            if(!$member){
                throw new ImportRowException(__('Member Tidak ditemukan'), 0, null, $row);
            };
            DB::beginTransaction();
            $data = [
                'member_id' => $member->id,
                'currency' => 'IDR',
                'plan_id' => $member->currentPlan->id,
                'total_claim' => $row['tot_amt_insurred'] ? $row['tot_amt_insurred'] : 0,
                'benefit_code' => $row['benefit_code'] ?  $row['benefit_code'] : '-',
                'benefit_desc' => $row['benefit_desc'] ?  $row['benefit_desc'] : '-',
                'amount_incurred' => $row['tot_amt_insurred'] ?  $row['tot_amt_insurred'] : 0,
                'amount_approved' => $row['tot_amt_approved'] ? $row['tot_amt_approved'] : 0,
                'amount_not_approved' => $row['tot_amt_not_approved'] ? $row['tot_amt_not_approved'] :0,
                'excess_paid' => $row['tot_excess_paid'] ? $row['tot_excess_paid'] : 0,
                'claim_request_id' => $claim_request_id,
                'organization_id' => $organization ? $organization->id : NULL,
                'status' => 'requested'
            ];


            $claimManagement = Claim::create($data);

            // update client id di claim request
            ClaimRequest::where('id', $claim_request_id)->update([
                'claim_id' => $claimManagement->id,
            ]);

            DB::commit();
            return $claimManagement;

            
        } catch (\Exception $error) {
            DB::rollBack();

            throw new \Exception($error);
        }
    }

    public static function updateClaimRequest($organization_id, $claim_request_id){
        try {
            $data = [
                'organization_id' => $organization_id
            ];
            DB::commit();
            $update = ClaimRequest::where('id', $claim_request_id)->update($data);
            return ClaimRequest::find($claim_request_id);

        } catch (\Exception $error) {
            DB::rollBack();

            throw new \Exception($error);
        }
    }

    protected function validatePlanRow($row)
    {
        if (empty($row['member_id'])) {
            throw new ImportRowException(__('Member ID Required'), 0, null, $row);
        }
    }

    public function handleRequestLogRow($row)
    {
        try {
            $requestLog = RequestLog::where('id', $row['id'])->first();
            if(!$requestLog){
                throw new ImportRowException(__('Request LOG Tidak ditemukan'), 0, null, $row);
            };
            $status = RequestLog::$status;

            if (!array_key_exists($row['status'], $status)){
                throw new ImportRowException(__('Status Request LOG Tidak ditemukan'), 0, null, $row);
            }

            // Update Request LOG Status & Link with Claim
            DB::beginTransaction();
                $requestLog->update([
                'status' =>  $row['status']
                ]);
                $requestLog->save();
            DB::commit();
            return $requestLog;
        } catch (\Exception $e) {
            DB::rollBack();

            throw $e;
        }
    }

}