<?php

namespace Modules\Internal\Transformers;

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

class AppointmentResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        $appointment = [
            'id' => $this->nID,
            'patient_name' => $this->user ? $this->user->full_name : '',
            'doctor_name' =>  $this->doctor ? $this->doctor->user?->full_name : '',
            'speciality' => $this->doctor->speciality->sKeterangan,
            'date_appointment' => Carbon::parse($this->appointmentDetail->dTanggalAppointment)->format('d-m-Y') . ' ' . $this->appointmentDetail->tTimeAppointment,
            'date_created' => Carbon::parse($this->dCreateOn)->format('d-m-Y H:i:s') ?? null,
            'appointment_media' => $this->sMedia,
            'status' => $this->status_name,
            'health_care' => $this->healthCare->sHealthCare ?? null,
            'payment_method' => $this->payment_method ?? null,
            'patient' => $this->user,
            'booking_code' => $this->sBookingCode,
            'his_detail' => [
                'RegID' => $this->sRegID,
                'Medrec' => $this->sNomorRekamMedis
            ],
            'type' => $this->type
        ];

        $payment_detail = null;
        if ($this->appointmentDetail->sPaymentDetails != null) {
            $payment_detail = [
                'payment_type' => $this->appointmentDetail->sPaymentDetails['payment_type'] ?? '',
                'transaction_time' => $this->appointmentDetail->sPaymentDetails['transaction_time'] ?? '',
                'gross_amount' => $this->appointmentDetail->sPaymentDetails['gross_amount'] ?? '',
                'currency' => $this->appointmentDetail->sPaymentDetails['currency'] ?? '',
                'status_message' => $this->appointmentDetail->sPaymentDetails['status_message'] ?? '',
            ];
        }

        $appointment['payment_detail'] = $payment_detail;

        return $appointment;
    }
}
