<?php

namespace App\Http\Resources;

use App\Facades\Settings;
use App\Facades\Site;
use Illuminate\Http\Resources\Json\JsonResource;
use Mtc\MercuryDataModels\Vehicle;

class AutoImagingResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    #[\Override]
    public function toArray($request)
    {
        return $this->resource
            ->map(fn (Vehicle $vehicle) => array_filter([
                'vrm' => $vehicle->registration_number,
                'vin' => $vehicle->vin,
                'make' => $vehicle->make?->name,
                'model' => $vehicle->model?->name,
                'variant' => $vehicle->derivative,
                'location' => $vehicle->dealership?->name,
                'image_count' => $vehicle->mediaUses()->count(),
                'colour' => $vehicle->colour,
                'transmission' => $vehicle->transmission?->name,
                'fuel-type' => $vehicle->fuelType?->name,
                'cap_id' => $vehicle->cap_id,
                'odometer' => $vehicle->odometer_mi,
                'url' => Site::vehicleUrl($vehicle),
                'EquipmentList' => Settings::get('image-sync-auto-imaging-include-equipment')
                    ? $vehicle->equipment->pluck('description')->implode(',')
                    : null,
            ], fn ($value) => $value !== null));
    }
}
