<?php

namespace App\Http\Resources;

use App\Facades\Feature;
use App\Facades\Settings;
use App\Modules\PlaceholderImages\ImaginStudio;
use App\Modules\Valuation\ConditionPriceTier;
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
            );
        }
        $prices = [
            'retailPrice' => $this->resource?->adjusted_retail_price ?? $this->resource?->retail_price,
            'belowPrice' => $this->resource?->adjusted_below_price ?? $this->resource?->below_price,
            'averagePrice' => $this->resource?->adjusted_average_price ?? $this->resource?->average_price,
            'cleanPrice' => $this->resource?->adjusted_clean_price ?? $this->resource?->clean_price,
        ];

        $selectedTier = ConditionPriceTier::fromCondition($this->resource?->cosmetic_condition);
        if ($selectedTier !== null) {
            $tierToApiKey = [
                ConditionPriceTier::TIER_CLEAN => 'cleanPrice',
                ConditionPriceTier::TIER_AVERAGE => 'averagePrice',
                ConditionPriceTier::TIER_BELOW => 'belowPrice',
            ];
            $selectedKey = $tierToApiKey[$selectedTier];
            if ($prices[$selectedKey] !== null) {
                $prices = [
                    'retailPrice' => null,
                    'belowPrice' => null,
                    'averagePrice' => null,
                    'cleanPrice' => null,
                    $selectedKey => $prices[$selectedKey],
                ];
            }
        }

        return [
            'id' => $this->resource?->id,
            'make' => $this->resource?->make,
            'model' => $this->resource?->model,
            'image' => $image,
            'registration' => $this->resource?->registration,
            'mileage' => $this->resource?->mileage,
            'derivative' => $this->resource?->derivative,
            'fuelType' => $this->resource?->fuel_type,
            'bodyType' => $this->resource?->body_type,
            'retailPrice' => $prices['retailPrice'],
            'belowPrice' => $prices['belowPrice'],
            'averagePrice' => $prices['averagePrice'],
            'cleanPrice' => $prices['cleanPrice'],
            'prices' => array_values(array_filter([
                ['key' => 'retailPrice', 'label' => 'Retail Price', 'price' => $prices['retailPrice']],
                ['key' => 'cleanPrice', 'label' => 'Clean Price', 'price' => $prices['cleanPrice']],
                ['key' => 'averagePrice', 'label' => 'Average Price', 'price' => $prices['averagePrice']],
                ['key' => 'belowPrice', 'label' => 'Below Average Price', 'price' => $prices['belowPrice']],
            ], fn (array $entry): bool => $entry['price'] !== null)),
            'valuation_id' => $this->resource?->valuation_id,
        ];
    }
}
