<?php

namespace Modules\Client\Transformers\AlarmCenter;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Carbon;

class DataServiceMonitoring extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'companyName' => $this->member->currentCorporate->name ?? null,
            'serviceCode' => $this->service_code ?? null,
            'memberId' => $this->member->member_id ?? null,
            'fullName' => $this->member->full_name ?? null,
            'dateOfBirth' => $this->member->birth_date ?? null,
            'phoneNumber' => $this->person->phone ?? null,
            'email' => $this->member->email ?? ($this->member->person->email ?? null),
            'serviceName' => $this->service->name ?? ($this->service_code ?? null),
            'benefits' => collect($this->requestLogBenefits)->map(function ($requestLogBenefit) {
                return [
                    'amountIncurred' => $requestLogBenefit->amount_incurred,
                    'amountApproved' => $requestLogBenefit->amount_approved,
                    'amountNotAprroved' => $requestLogBenefit->amount_not_approved,
                    'excessPaid' => $requestLogBenefit->excess_paid,
                    'description' => $requestLogBenefit->keterangan,
                    'name' => $requestLogBenefit->benefit->description,
                ];
            })->all() ?? null,
            'benefitTotal' => $this->benefitTotal ?? null,
            'hospital' => $this->organization->name ?? null,
            'admissionDate' => $this->submission_date ?? null,
            'dischargeDate' => $this->discharge_date ?? null,
            'dailyMonitorings' => $this->when($this->service_code === 'IP', collect($this->requestLogDailyMonitorings)
                ->groupBy(function ($requestLogDailyMonitoring) {
                    return $requestLogDailyMonitoring->created_at->format('d M Y');
                })
                ->map(function ($groupedItems) {
                    return collect($groupedItems)
                        ->map(function ($requestLogDailyMonitoring) {
                            return [
                                'time' => $requestLogDailyMonitoring->created_at->format('H:i') ?? null,
                                'status' => 'Done' ?? null,
                                'subject' => $requestLogDailyMonitoring->subject ?? null,
                                'bodyTemperature' => $requestLogDailyMonitoring->body_temperature ?? null,
                                'sistole' => $requestLogDailyMonitoring->sistole . 'mm[Hg]' ?? null,
                                'diastole' => $requestLogDailyMonitoring->diastole . 'mm[Hg]' ?? null,
                                'respirationRate' => $requestLogDailyMonitoring->respiration_rate . '/min' ?? null,
                                'analysis' => $requestLogDailyMonitoring->analysis ?? null,
                                'complaints' => $requestLogDailyMonitoring->complaints ?? null,
                                'plans' => $this->when($requestLogDailyMonitoring->requestLogMedicalPlans, collect($requestLogDailyMonitoring->requestLogMedicalPlans)
                                    ->map(function ($requestLogMedicalPlan) {
                                        return [
                                            'type' => $requestLogMedicalPlan->type,
                                            'plan' => $requestLogMedicalPlan->plan
                                        ];
                                    })
                                    ->sortBy('type')
                                    ->all()) ?? null,
                            ];
                        })
                        ->sortByDesc(function ($item) {
                            return $item['time'];
                        })
                        ->values();
                })
                ->sortByDesc(function ($groupedItems, $date) {
                    return Carbon::createFromFormat('d M Y', $date)->format('Y-m-d');
                })
                ->all()) ?? null,
            'laboratoriumResults' => $this->whenLoaded('requestLogDailyMonitorings', collect($this->requestLogDailyMonitorings)
                ->groupBy(function ($requestLogDailyMonitoring) {
                    return Carbon::parse($requestLogDailyMonitoring->lab_date)->format('d M Y');
                })
                ->map(function ($groupedItems) {
                    return collect($groupedItems)
                        ->map(function ($requestLogDailyMonitoring) {
                            return [
                                'code' => $requestLogDailyMonitoring->code,
                                'date' => Carbon::parse($requestLogDailyMonitoring->lab_date)->format('d M Y') ?? null,
                                'examination' => $requestLogDailyMonitoring->examination ?? null,
                                'location' => $requestLogDailyMonitoring->provider ?? null,
                            ];
                        })
                        ->sortByDesc(function ($item) {
                            return $item['code'];
                        })
                        ->values();
                })
                ->sortByDesc(function ($groupedItems, $date) {
                    return Carbon::createFromFormat('d M Y', $date)->format('Y-m-d');
                })
                ->all()) ?? null
        ];
    }
}
