<?php

namespace App\Modules\VehicleTrims\Http\Resources;

use App\Facades\Site;
use App\Http\Resources\SeoDataForModel;
use App\Http\Resources\Traits\TransformsImageAttributesForAdmin;
use App\VehicleTrimRepository;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\App;
use Mtc\ContentManager\Contracts\MediaUse;
use Mtc\ContentManager\Http\Resources\MediaEntryResource;
use Mtc\ContentManager\Models\Template;
use Mtc\ContentManager\Traits\MapContentTrait;
use Mtc\MercuryDataModels\Traits\HasVehicleCustomAttributes;
use Mtc\MercuryDataModels\NewCar;
use Mtc\MercuryDataModels\VehicleMake;
use Mtc\MercuryDataModels\VehicleModel;

class VehicleTrimView extends JsonResource
{
    use HasVehicleCustomAttributes;
    use MapContentTrait;
    use SeoDataForModel;
    use TransformsImageAttributesForAdmin;

    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)
    {
        $this->resource->load([
            'content.mediaUses.media',
            'content.subContent.mediaUses.media',
            'content.subContent.subContent.mediaUses.media',
            'content.subContent.subContent.subContent.mediaUses.media',
            'content.globalContent.contentElement.fields',
            'content.contentElement.fields',
            'content.subContent.contentElement.fields',
            'content.subContent.subContent.contentElement.fields',
            'content.subContent.subContent.subContent.contentElement.fields',
            'attributeValues',
            'mediaUses.media',
        ]);

        $this->resource->data = $this->getDataValues();

        $trim = $this->resource->toArray();

        $trim['structure_in_sync'] = $this->resource->template_id
            ? $this->repository()->isContentOutOfSync($this->resource) !== true
            : true;

        $trim['content'] = $this->resource->content
            ->filter(fn ($content) => !empty($content->slug))
            ->map(fn ($content) => $this->mapContent($content))
            ->values();

        $trim['templates'] = Template::query()->active()->select('name', 'id')->get();
        $trim['extra'] = $this->transformImageAttributesForAdmin($this->getExtraData($this->resource)->toArray());
        $trim['image'] = $this->resource->mediaUses->where('primary', 1)->pluck('media_id');
        $trim['image_uses'] = $this->resource->mediaUses->where('primary', 1)
            ->map(fn (MediaUse $mediaUse) => new MediaEntryResource($mediaUse))->values();
        $trim['listing_image'] = $this->resource->mediaUses->where('primary', 0)->pluck('media_id');
        $trim['listing_image_uses'] = $this->resource->mediaUses->where('primary', 0)
            ->map(fn (MediaUse $mediaUse) => new MediaEntryResource($mediaUse))->values();
        $trim['seo'] = $this->getSeoData($this->resource);
        $trim['site_url'] = Site::trimUrl($this->resource, true);

        return $trim;
    }

    private function getDataValues()
    {
        $data = $this->resource->data ?? [];

        $data['options']['make_ids'] = VehicleMake::query()
            ->orderBy('name')
            ->get()
            ->map(fn ($option) => [
                'id' => (int) $option->id,
                'name' => $option->name
            ])->toArray();

        $data['options']['model_ids'] = VehicleModel::query()
            ->orderBy('name')
            ->get()
            ->map(fn ($option) => [
                'id' => (int) $option->id,
                'name' => $option->name
            ])->toArray();

        $data['options']['new_car_ids'] = NewCar::query()
            ->orderBy('name')
            ->get()
            ->map(fn ($option) => [
                'id' => (int) $option->id,
                'name' => $option->name
            ])->toArray();

        return $data;
    }

    private function repository(): VehicleTrimRepository
    {
        return App::make(VehicleTrimRepository::class);
    }
}
