<?php

namespace App\Http\Resources;

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

class QueueEntryResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'queue_date' => $this->queue_date?->toDateString(),
            'token_number' => $this->token_number,
            'status' => $this->status?->value,
            'arrival_at' => $this->arrival_at?->toIso8601String(),
            'sent_to_doctor_at' => $this->sent_to_doctor_at?->toIso8601String(),
            'completed_at' => $this->completed_at?->toIso8601String(),
            'patient' => $this->whenLoaded('patient', fn () => [
                'id' => $this->patient->id,
                'patient_code' => $this->patient->patient_code,
                'name' => $this->patient->name,
                'phone' => $this->patient->phone,
            ]),
            'doctor' => $this->whenLoaded('doctor', fn () => [
                'id' => $this->doctor->id,
                'name' => $this->doctor->user?->name,
            ]),
            'location' => $this->whenLoaded('location', fn () => [
                'id' => $this->location->id,
                'name' => $this->location->name,
            ]),
            'appointment' => $this->whenLoaded('appointment', fn () => [
                'id' => $this->appointment->id,
                'appointment_code' => $this->appointment->appointment_code,
                'status' => $this->appointment->status?->value,
                'payment_status' => $this->appointment->payment_status?->value,
            ]),
        ];
    }
}
