<?php

namespace Modules\Internal\Transformers;

use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Helpers\Helper;

class LivechatResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        $livechat = [
            'id' => $this->nID,
            'doctor_name' => isset($this->doctor->user->sFirstName) ? $this->doctor->user->detail->sTitlePrefix . '. ' . $this->doctor->user->sFirstName . ' ' . $this->doctor->user->sLastName . ' ' . $this->doctor->user->detail->sTitleSuffix : null,
            'pasien_name' => isset($this->user->sFirstName) ? $this->user->sFirstName . ' ' . $this->user->sLastName : null,
            'pasien_phone' => isset($this->user->sPhone) ? $this->user->sPhone : '-',
            'pasien_email' => isset($this->user->sEmail) ? $this->user->sEmail : '-',
            'speciality' => $this->doctor->speciality->sKeterangan ?? null,
            'health_care' => $this->healthCare->sHealthCare ?? null,
            'date_appointment' => $this->appointment !== null ? 
                Carbon::parse($this->appointment->appointmentDetail->dTanggalAppointment)->format('d-m-Y') 
                . ' ' . $this->appointment->appointmentDetail->tTimeAppointment :
                null,
            'status_appointment' => $this->appointment->paymentStatus ?? null,
            'date_created' => Carbon::parse($this->dCreateOn)->format('d-m-Y') ?? null,
            'time_request' => Carbon::parse($this->dCreateOn)->format('H:i:s') ?? null,
            'patient_media' => $this->sMedia ?? null,
            'doctor_media' => $this->sMediaDokter ?? null,
            'appointment_media' => $this->appointment->sMedia ?? null,
            'status_chat' => $this->status_name ?? null,
            'payment_method' => $this->appointment->payment_method ?? null,
        ];

        $start_time = $this->dStartTime;
        $end_time = $this->dEndTime;
        $data_duration = 0 . ' jam ' . 0 . ' menit ' . 0 . ' detik';
        if ($start_time != null && $end_time != null) {
            $duration = Carbon::parse($start_time)->diffInMinutes(Carbon::parse($end_time));
            $hours = floor($duration / 60);
            $minutes = $duration % 60;
            $seconds = ($duration - ($hours * 60) - $minutes) * 60;

            $data_duration = $hours . ' jam ' . $minutes . ' menit ' . $seconds . ' detik';
        }

        $livechat['duration'] = $data_duration;

        $payment_detail = null;

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

        $livechat['payment_detail'] = $payment_detail;
        return $livechat;
    }
}
