<?php

namespace App\Http\Resources;

use App\Models\Booking;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
 * @mixin Booking
 */
class BookingResource extends JsonResource
{
    /**
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'number' => $this->number,
            'customerId' => $this->customer_id,
            'agentId' => $this->agent_id,
            'unitId' => $this->unit_id,
            'unitSubtypeId' => $this->unit_subtype_id,
            'floorId' => $this->floor_id,
            'unitNumber' => $this->unit_number,
            'customer' => $this->whenLoaded(
                'customer',
                fn (): array => [
                    'id' => $this->customer->id,
                    'name' => trim($this->customer->first_name.' '.$this->customer->last_name),
                    'phone' => $this->customer->phone,
                    'email' => $this->customer->email,
                    'status' => $this->customer->status->value,
                    'nominee' => $this->customer->nominee === null
                        ? null
                        : NomineeResource::make($this->customer->nominee),
                ],
            ),
            'agent' => $this->whenLoaded(
                'agent',
                fn (): ?array => $this->agent === null
                    ? null
                    : [
                        'id' => $this->agent->id,
                        'name' => $this->agent->name,
                        'phone' => $this->agent->phone,
                    ],
            ),
            'unit' => $this->whenLoaded(
                'unit',
                fn (): array => [
                    'id' => $this->unit->id,
                    'name' => $this->unit->name,
                ],
            ),
            'subtype' => $this->whenLoaded(
                'subtype',
                fn (): array => [
                    'id' => $this->subtype->id,
                    'name' => $this->subtype->name,
                ],
            ),
            'floor' => $this->whenLoaded(
                'floor',
                fn (): array => [
                    'id' => $this->floor->id,
                    'name' => $this->floor->name,
                ],
            ),
            'downPayment' => $this->down_payment,
            'receivedAmount' => $this->received_amount,
            'paymentType' => $this->payment_type?->value,
            'paymentMethod' => $this->payment_method?->value,
            'paymentReference' => $this->payment_reference,
            'accountName' => $this->account_name,
            'documents' => BookingDocumentResource::collection($this->whenLoaded('documents')),
            'status' => $this->status->value,
            'createdBy' => $this->whenLoaded(
                'creator',
                fn (): array => [
                    'id' => $this->creator->id,
                    'name' => $this->creator->name,
                    'email' => $this->creator->email,
                ],
                $this->created_by,
            ),
            'createdAt' => $this->created_at?->toIso8601String(),
            'updatedAt' => $this->updated_at?->toIso8601String(),
        ];
    }
}
