<?php

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

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

class ProductShowResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request): array
    {
        $reviews = $this->whenLoaded('reviews');
        $categories = $this->whenLoaded('categories');

        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' => $this->backorder ? 'true' : 'false',
            'weight_value' => $this->weight_value,
            'weight_unit' => $this->weight_unit,
            'height_value' => $this->height_value,
            'height_unit' => $this->height_unit,

            'media' => MediaResource::collection($this->whenLoaded('media')),

            'created_at' => $this->created_at,

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

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

            'available_colors' => PropertyRepository::getAvailableColors($this),
            'available_sizes' => PropertyRepository::getAvailableSizes($this),

            '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')),
            'reviews' => [
                'count' => $reviews ? $reviews->count() : 0,
                'rating' => $reviews ? number_format($reviews->avg('rating'), 2) : 0,
            ],

            'reviews_resources' => ProductReviewResource::collection($this->whenLoaded('reviews')),

            $this->mergeWhen($categories, [
                'categories' => $categories->map(fn ($category) => [
                    'id' => $category->id,
                    'name' => $category->name,
                ]),
            ]),
        ];
    }
}
