<?php

namespace App\Http\Resources;

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

class VehicleMoveRequestView 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',
            'fromDealership',
            'toDealership',
            'processingLogs',
        ]);
        if (empty($this->resource->payment_reference)) {
            $this->resource->payment_reference = $this->resource->invoice?->payments?->first()?->reference;
        }
        $paidAt = $this->resource->paid_at;
        $teamNotifiedAt = $this->resource->team_notified_at;
        $resource = parent::toArray($request);
        $resource['paid_at'] = $paidAt?->format('d/m/Y H:i');
        $resource['team_notified_at'] = $teamNotifiedAt?->format('d/m/Y H:i');
        $resource['from_dealership_name'] = $this->resource->fromDealership?->name;
        $resource['to_dealership_name'] = $this->resource->toDealership?->name;
        $resource['processing_logs'] = ProcessingLogResource::collection($this->resource->processingLogs);
        return $resource;
    }
}
