<?php

namespace App\Http\Resources;

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

class PatientProfileResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'patient_code' => $this->patient_code,
            'full_name' => $this->fullName(),
            'email' => $this->email,
            'phone' => $this->phone,
            'whatsapp' => $this->whatsapp,
            'status' => $this->status,
            'upcoming_appointments' => AppointmentResource::collection(
                $this->appointments()->where('appointment_date', '>=', now())->orderBy('appointment_date')->limit(5)->get()
            ),
            'past_appointments' => AppointmentResource::collection(
                $this->appointments()->where('appointment_date', '<', now())->orderByDesc('appointment_date')->limit(5)->get()
            ),
            'invoices' => InvoiceResource::collection($this->invoices()->orderByDesc('invoice_date')->limit(10)->get()),
            'payments' => PaymentResource::collection(
                Payment::whereIn('invoice_id', $this->invoices->pluck('id'))->orderByDesc('payment_date')->limit(10)->get()
            ),
            'balance' => $this->invoices->sum('balance'),
            'active_packages' => PatientPackageResource::collection(
                $this->packages()->where('status', 'active')->get()
            ),
        ];
    }
}
