<?php

namespace App\Http\Resources;

use App\Traits\MediaHasSizes;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\Resources\Json\JsonResource;
use Mtc\MercuryDataModels\VehicleTrim;

class VehicleTrimSearchListResource extends JsonResource
{
    use MediaHasSizes;

    public static $wrap = '';

    #[\Override]
    public function toArray($request)
    {
        if ($this->resource instanceof LengthAwarePaginator) {
            $this->resource->through(fn (VehicleTrim $trim) => $this->map($trim));
        } else {
            $this->resource = $this->resource->map(fn (VehicleTrim $trim) => $this->map($trim));
        }

        return parent::toArray($request);
    }

    protected function map(VehicleTrim $trim): array
    {
        $trimImage = $trim->mediaUses
            ->where('primary', true)
            ->first();

        $listingImage = $trim->mediaUses
            ->filter(fn ($mediaUse) => $mediaUse->flags['listing'] ?? null)
            ->first();

        if ($trimImage) {
            $trimImage->setOverrideOwnerType('new-car');
        }

        if ($listingImage) {
            $listingImage->setOverrideOwnerType('new-car');
        }

        $trimImageSizes = !empty($trimImage)
            ? $this->allSizesForUse($trim, $trimImage)
                ->put('original', $trimImage->media->getOriginalUrlAttribute())
            : [];

        $listingImageSizes = !empty($listingImage)
            ? $this->allSizesForUse($trim, $listingImage)
                ->put('original', $listingImage->media->getOriginalUrlAttribute())
            : [];

        return [
            'id' => $trim->id,
            'name' => $trim->name,
            'slug' => $trim->slug,
            'make_name' => $trim->make?->name,
            'make_slug' => $trim->make?->slug,
            'model_name' => $trim->model?->name,
            'model_slug' => $trim->model?->slug,
            'new_car_slug' => $trim->newCar?->slug,
            'description' => $trim->description,
            'full_price' => $trim->full_price,
            'fuel_types' => $trim->fuel_types,
            'transmission_types' => $trim->transmission_types,
            'images' => [
                'trim' => [
                    'id' => $trimImage?->id,
                    'title' => $trimImage?->title,
                    'alt_text' => $trimImage?->alt_text,
                    'description' => $trimImage?->description,
                    'caption' => $trimImage?->caption,
                    'interior' => $trimImage?->interior,
                    'exterior' => $trimImage?->exterior,
                    'hex1' => $trimImage?->media?->hex1,
                    'hex2' => $trimImage?->media?->hex2,
                    'type' => $trimImage?->media?->type,
                    'src' => $trimImage?->media?->getOriginalUrlAttribute(),
                    'sizes' => $trimImageSizes,
                ],
                'listing' => [
                    'id' => $listingImage?->id,
                    'title' => $listingImage?->title,
                    'alt_text' => $listingImage?->alt_text,
                    'description' => $listingImage?->description,
                    'caption' => $listingImage?->caption,
                    'interior' => $listingImage?->interior,
                    'exterior' => $listingImage?->exterior,
                    'hex1' => $listingImage?->media?->hex1,
                    'hex2' => $listingImage?->media?->hex2,
                    'type' => $listingImage?->media?->type,
                    'src' => $listingImage?->media?->getOriginalUrlAttribute(),
                    'sizes' => $listingImageSizes,
                ],
            ],
            'custom' => $trim->getAllCustom(),
        ];
    }
}
