<?php

namespace App\Http\Resources\Api\V1\Vendor\Product;

use App\Http\Controllers\Api\V1\Product\Resources\Attribute\ProductAttributeProductShowResource;
use App\Http\Controllers\Api\V1\Product\Resources\ProductMediaResource;
use App\Http\Controllers\Api\V1\Product\Resources\Variant\ProductVariantResource;
use App\Http\Resources\Api\V1\Channel\ChannelResource;
use App\Repositories\Ecommerce\Product\Property\PropertyRepository;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class VendorProductIndexResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $media = $this->whenLoaded('media');

        return [
            'id' => $this->id,
            'parent_id' => $this->parent_id,

            'name' => $this->name,
            'slug' => $this->slug,
            'description' => $this->description,

            'sku' => $this->sku,
            'barcode' => $this->barcode,
            'stock' => $this->stock,

            'price_amount' => $this->price_amount,
            'old_price_amount' => $this->old_price_amount,

            'backorder' => boolval($this->backorder),
            'weight_value' => $this->weight_value,
            'weight_unit' => $this->weight_unit,
            'height_value' => $this->height_value,
            'height_unit' => $this->height_unit,

            'media' => ProductMediaResource::collection($media),

            'created_at' => $this->created_at,

            'seo_title' => $this->seo_title,
            'seo_description' => $this->seo_description,

            'is_visible' => $this->is_visible,

            'colour' => PropertyRepository::getRealValue('colour', $this->colour),
            'size' => PropertyRepository::getRealValue('size', $this->size),

            'brand' => [
                'id' => $this->brand_id,
                'name' => $this->brand?->name,
            ],

            'channel' => ChannelResource::collection($this->whenLoaded('channels')),

            'properties' => ProductAttributeProductShowResource::collection($this->whenLoaded('properties')),
            'variations' => ProductVariantResource::collection($this->whenLoaded('variations')),
        ];
    }
}
