<?php

namespace Modules\Product\Models\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Modules\Attribute\Models\Resources\AttributeResource;
use Modules\Brand\Models\Resources\BrandResource;
use Modules\Category\Models\Resources\CategoryResource;
use Modules\Product\Services\ProductService;
use Modules\Region\Models\Resources\RegionResource;
use Modules\Store\Models\Resources\StoreResource;

class ProductResource extends JsonResource
{

    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $images = $this->photos->pluck('image')->toArray();
         array_unshift($images, $this->image);
        $images = array_map(function($image) {
            return asset($image);
        }, $images);


        return [
            'id' => $this->id,
            'code' => $this->code,
            'status_active' => $this->status_active,
            'image' => asset($this->image),
            'images' => $images,
            'max_count_order' => $this->max_count_order,
            'discount_percentage' => $this->discount_percentage,
            'views' => $this->views,
            'cost'=>$this->price,
            'favorited'=>$this->favorited() ? true : false,
            'can_rate'=>$this->rated() ? false : true,
            'rate'=>$this->rate,
            'rate_count'=>$this->rate_count,
            'title'=>$this->currentDescription->title,
            'description'=>$this->currentDescription->description,
            'items'=>ProductItemResource::collection($this->whenLoaded('items')),
            'productOptions'=>ProductWarehouseResource::collection($this->whenLoaded('productOptions')),
            'attribute'=>new AttributeResource($this->whenLoaded('attribute')),
            'brand'=>new BrandResource($this->whenLoaded('brand')),
            'category'=>new CategoryResource($this->whenLoaded('category')),
            'subCategory'=>new CategoryResource($this->whenLoaded('subCategory')),
            'store'=>new StoreResource($this->whenLoaded('store')),
        ];
    }
}
