<?php

namespace App\Http\Resources;

use App\Facades\Feature;
use App\Facades\Settings;
use App\Modules\PlaceholderImages\ImaginStudio;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\App;

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

    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    #[\Override]
    public function toArray($request)
    {
        if (is_array($this->resource) && isset($this->resource[0]['variant_id'])) {
            return $this->resource;
        }

        $image = null;
        if (
            Feature::isEnabled('imagin-studio-placeholders')
            && Settings::get('imagin-placeholders-valuation-lookup-enabled')
        ) {
            $image = App::make(ImaginStudio::class)->getForOther(
                $this->resource->make,
                $this->resource->model,
                $this->resource->colour
            );
        }
        return [
            'id' => $this->resource?->id,
            'make' => $this->resource?->make,
            'model' => $this->resource?->model,
            'image' => $image,
            'name' => $this->resource?->derivative,
            'colour' => $this->resource?->colour,
            'registration' => $this->resource?->registration,
            'mileage' => $this->resource?->mileage,
            'derivative' => $this->resource?->derivative,
            'fuel_type' => $this->resource?->fuel_type,
            'body_type' => $this->resource?->body_type,
            'retail_price' => $this->resource?->adjusted_retail_price ?? $this->resource?->retail_price,
            'below_price' => $this->resource?->adjusted_below_price ?? $this->resource?->below_price,
            'average_price' => $this->resource?->adjusted_average_price ?? $this->resource?->average_price,
            'clean_price' => $this->resource?->adjusted_clean_price ?? $this->resource?->clean_price,
            'valuation_id' => $this->resource?->valuation_id,
        ];
    }
}
