<?php

namespace App\Http\Resources;

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

class ReservationView extends JsonResource
{
    public static $wrap = '';

    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    #[\Override]
    public function toArray(Request $request): array
    {
        $this->resource->load([
            'vehicle' => function ($query) {
                $query->withTrashed();
            },
            'vehicle.primaryMediaUse.media',
            'vehicle.dealership',
        ]);
        if (empty($this->resource->payment_reference)) {
            $this->resource->payment_reference = $this->resource->invoice?->payments?->first()->reference;
        }
        $confirmedAt = $this->resource->confirmed_at;
        $confirmationSentAt = $this->resource->confirmation_sent_at;
        $ingestedAt = $this->resource->ingested_at;
        $resource = parent::toArray($request);
        $resource['confirmed_at'] = $confirmedAt?->format('d/m/Y H:i');
        $resource['confirmation_sent_at'] = $confirmationSentAt?->format('d/m/Y H:i');
        $resource['ingested_at'] = $ingestedAt?->format('d/m/Y H:i');
        return $resource;
    }
}
