<?php

namespace TPS\API\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;
use TPS\API\Http\Resources\Catalog\ProductVariant;
use TPS\API\Http\Resources\Catalog\SuperAttribute;
use TPS\Shop\Repositories\ProductRepository;
use Webkul\RestApi\Http\Controllers\V1\Shop\Catalog\ProductController;
use TPS\API\Http\Resources\Catalog\Product as ProductResource;

class Products extends ProductController
{
    /**
     * Resource class name.
     *
     * @return string
     */
    public function resource()
    {
        return ProductResource::class;
    }

    public function repository()
    {
        return ProductRepository::class;
    }
    /**
     * Returns an individual resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getResource(Request $request, int $id)
    {
        $resourceClassName = $this->resource();

        return new $resourceClassName($this->getRepositoryInstance()->findOrFail($id));
    }

    public function variants($id){
        $product = $this->getRepositoryInstance()->findOrFail($id);

        if($product->super_attributes->isNotEmpty() && $product->variants->isNotEmpty()){
            $attribute = $product->super_attributes->first();
            $distinctVariants =  $product->variants->unique($attribute->code);

            $gr_data = array(
                'attribute' => SuperAttribute::make($attribute),
                'options' =>[],
                'level' => $product->super_attributes->count()
            );

            foreach($distinctVariants as $variant){
                $option = $attribute->options->firstWhere('id',$variant->{$attribute->code});

                $item = [
                    'option' => $option->admin_name,
                    'images' => $variant->images,
                ];

                $attributes = $product->super_attributes;
                if($attributes->count()>1 && $option)
                {
                    $last_attribute = $product->super_attributes->last();

                    $products =  $product->variants->where($attribute->code,$variant->{$attribute->code})
                        ->map(function ($item,$key) use ($attributes){

                            return ProductVariant::make($item,$attributes);
                        });

                    $item['variants']['attribute'] = SuperAttribute::make($last_attribute);
                    $item['variants']['products'] = $products->values();
                }
                else
                {
                    $item['product'] = ProductVariant::make($variant,$option);
                }
                $gr_data['options'][] = $item;
            }
            return response()->json($gr_data);
        }
        return response()->json(['message' => 'not found'],404);
    }

    public function newProducts(){
        $count = core()->getConfigData('catalog.products.homepage.no_of_featured_product_homepage');
        $products =  $this->getRepositoryInstance()->getNewProducts(10);
        return $this->getResourceCollection($products);
    }

    public function featuredProducts(){

        $products =  $this->getRepositoryInstance()->getFeaturedProducts(10);
        return $this->getResourceCollection($products);
    }

    public function discountedProducts(){
        $products =  $this->getRepositoryInstance()->getDiscountedProducts(10);
        return $this->getResourceCollection($products);
    }

}