<?php

namespace App\Http\Resources;

use App\Http\Controllers\SearchFacetController;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Mtc\MercuryDataModels\BodyStyleType;
use Mtc\MercuryDataModels\Colour;
use Mtc\MercuryDataModels\Franchise;
use Mtc\MercuryDataModels\FuelType;
use Mtc\MercuryDataModels\TransmissionType;
use Mtc\MercuryDataModels\VehicleMake;
use Mtc\MercuryDataModels\VehicleModel;

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

    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'position' => $this->position,
            'filter_type' => $this->filter_type,
            'filter_name' => SearchFacetController::getFilterName($this->filter_type),
            'example_values' => $this->getExampleValues($this->filter_type),
            'robots_index' => $this->robots_index,
            'robots_follow' => $this->robots_follow,
        ];
    }

    private function getExampleValues(string $filterType): string
    {
        $examples = match ($filterType) {
            'make' => VehicleMake::query()->inRandomOrder()->where('cars', 1)->limit(5)->pluck('name')->toArray(),
            'model' => VehicleModel::query()->inRandomOrder()->ofType('car')->limit(5)->pluck('name')->toArray(),
            'body_type' => BodyStyleType::query()->inRandomOrder()->limit(5)->pluck('name')->toArray(),
            'colour' => Colour::query()->inRandomOrder()->limit(3)->pluck('name')->toArray(),
            'fuel_type' => FuelType::query()->inRandomOrder()->limit(3)->pluck('name')->toArray(),
            'transmission' => TransmissionType::query()->inRandomOrder()->limit(3)->pluck('name')->toArray(),
            'franchise' => Franchise::query()->inRandomOrder()->limit(3)->pluck('name')->toArray(),
            'location' => ['London', 'Manchester', 'Birmingham'],
            default => [],
        };

        return implode(', ', $examples);
    }
}
