<?php

namespace Modules\Internal\Transformers;

use App\Models\Plan;
use App\Models\CorporateBenefit;
use App\Models\MemberPlan;
use App\Models\CorporateService;
use App\Models\RequestLogBenefit;
use App\Models\RequestLogMedicine;
use App\Models\Organization;
use App\Models\Exclusion;
use App\Models\Icd;
use App\Helpers\Helper;
use App\Models\CorporatePolicy;
use Illuminate\Http\Resources\Json\JsonResource;

class RequestLogShowResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {

        $requestLog = parent::toArray($request);
        $corporateId = $requestLog['member']['current_plan']['corporate_id'] ?? 0;
        $member_id = $requestLog['member_id'];
        $planMember = MemberPlan::where('member_id', $member_id)->get('plan_id');

        $planId = Plan::whereIn('id', $planMember)->where('service_code', $requestLog['service_code'])->first();
        $benefit = CorporateBenefit::with('benefit')->where('plan_id', $planId->id)->get()->toArray();
        $benefitDetailLog = RequestLogBenefit::with('benefit')->where('request_log_id', $requestLog['id'])->get()->toArray();
        $medicineDetailLog = RequestLogMedicine::where('request_log_id', $requestLog['id'])->get()->toArray();
        $provider = Organization::where('id', $requestLog['organization_id'])->first();

        if ($provider){
            $providerName = $provider->name;
        } else {
            $providerName = '-';
        }

        $benefitData = [];

        if (count($benefit)){
            foreach($benefit as $data){
                array_push($benefitData, $data['benefit']);
            }
        }

        // Medicine
        $medicineData = [];
        if (count($medicineDetailLog)){
            foreach($medicineDetailLog as $data){
                array_push($medicineData, $data);
            }
        }

        // Service Rule
        $corporateService = CorporateService::query()
            ->where('corporate_id', $corporateId)
            ->where('service_code', $requestLog['service_code'])
            ->with(['configs'])
            ->first();
        $config = [];
        if ($corporateService) {
            $config = $corporateService->configs->pluck('value', 'name')->toArray();
        }

        // Exclusion Service or diagnosis
        $exclusions = Exclusion::query()
            ->where('corporate_id', $corporateId)
            ->where('type', 'diagnosis')
            ->with(['exclusionable', 'rules'])
            ->get()->toArray();

        // Policy Number
        $policyNumber = CorporatePolicy::query()
            ->where('corporate_id', $corporateId)
            ->first();

        // Diagnosis
        $icd = [];
        if($requestLog['diagnosis']){
            $diagnosis = explode(',', $requestLog['diagnosis']);
            $icd = Icd::query()
                    ->whereIn('code', $diagnosis)
                    ->select('code', 'name')
                    ->get();
        }

        $data = [
            'id' => $requestLog['id'],
            'code' => $requestLog['code'],
            'member_id' => $requestLog['member']['member_id'],
            'corporate_id' => $corporateId,
            'policy_number' =>$policyNumber->code ? $policyNumber->code : '-',
            'name' => $requestLog['member']['name'],
            'date_of_birth' => $requestLog['member']['birth_date'],
            'gender' => $requestLog['member']['gender'],
            'marital_status' => $requestLog['member']['status_marital'],
            'member_type' => Helper::memberType($requestLog['member']['record_type']),
            'principal_id' => $requestLog['member']['principal_id'] ? $requestLog['member']['principal_id'] : '-',
            'principal_name' => $requestLog['member']['principal_id'] ? Helper::principalName($requestLog['member']['principal_id']) : '-',
            'relation_with_principal' => Helper::relationWithPrincipal($requestLog['member']['relation_with_principal']),
            'submission_date' => $requestLog['submission_date'],
            'discharge_date' => $requestLog['discharge_date'],
            'service_type' => Helper::serviceName($requestLog['service_code']),
            'claim_method' => $requestLog['payment_type'],
            'status' => $requestLog['status'],
            'status_final_log' => $requestLog['status_final_log'],
            'benefit' => $benefitData,
            'benefit_data' => $benefitDetailLog,
            'config_service' => $config,
            'exclusion' => $exclusions,
            'medicine' => $medicineData,
            'files' => $requestLog['files'],
            'provider' => $providerName,
            'no_identitas' => $requestLog['member']['nric'],
            'keterangan' => $requestLog['keterangan'],
            'hak_kamar_pasien' => $requestLog['hak_kamar_pasien'],
            'penempatan_kamar' => $requestLog['penempatan_kamar'],
            'catatan' => $requestLog['catatan'],
            'reason' => $requestLog['reason'],
            'diagnosis' => $icd,

        ];

        return $data;
    }
}
