<?php

namespace App\Http\Controllers\Api\V1\Product\Resources\Variant;

use App\Http\Controllers\Api\V1\Product\Resources\Attribute\ProductAttributeResource;
use App\Repositories\Ecommerce\Product\Property\PropertyRepository;
use Illuminate\Http\Resources\Json\JsonResource;

class ProductVariantResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        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,

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

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

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

            'thumbnail' => $this->thumbnail(),
            'images_400x400' => $this->getMedia('uploads')->map(fn ($media) => $media->getUrl('thumb400x400')),
            'images_800x800' => $this->getMedia('uploads')->map(fn ($media) => $media->getUrl('thumb800x800')),
            'images_1200x1200' => $this->getMedia('uploads')->map(fn ($media) => $media->getUrl('thumb1200x1200')),

            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,

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