<?php

namespace Yas\FrontTheme\Http\Controllers\API;

use Illuminate\Http\Resources\Json\JsonResource;
use Webkul\Category\Repositories\CategoryRepository;
use Webkul\Marketing\Jobs\UpdateCreateSearchTerm as UpdateCreateSearchTermJob;
use Webkul\Product\Repositories\ProductRepository;
use Yas\FrontTheme\Http\Resources\ProductResource;

class ProductController extends APIController
{
    /**
     * Create a controller instance.
     *
     * @return void
     */
    public function __construct(
        protected CategoryRepository $categoryRepository,
        protected ProductRepository $productRepository
    ) {}

    /**
     * Product listings.
     */
    public function index(): JsonResource
    {
        
        if (core()->getConfigData('catalog.products.search.engine') == 'elastic') {
            $searchEngine = core()->getConfigData('catalog.products.search.storefront_mode');
        }

        $queryParams = request()->query();
        if (isset($queryParams['category_id'])) {
            $categoryId = $queryParams['category_id'];
           
            $category = $this->categoryRepository->where('id', $categoryId)->first();

            if ($category && $category->parent_id == 1) {
                $subCategoryIds = $this->categoryRepository
                    ->where('parent_id', $categoryId)
                    ->pluck('id')
                    ->toArray();
            
                $categoryIds =  $subCategoryIds;
            } else {
                $categoryIds = [$categoryId];
            }

            unset($queryParams['category_id']);
           
            $products = $this->productRepository
            ->whereHas('companies', function($query) {
                $query->where("status", 1);
            })
            ->whereHas('categories', function ($query) use ($categoryIds) {
                $query->whereIn('categories.id', $categoryIds);
            })
            ->with('inventories','ordered_inventories','active_inventories')
            ->setSearchEngine($searchEngine ?? 'database')
            ->getAll(array_merge($queryParams, [
                'channel_id'           => core()->getCurrentChannel()->id,
                'status'               => 1,
                'visible_individually' => 1,
            ]));
            
        }else{
            $products = $this->productRepository
            ->whereHas('companies', function($query) {
                $query->where("status", 1);
            })
            ->with('inventories','ordered_inventories','active_inventories')
            ->setSearchEngine($searchEngine ?? 'database')
            ->getAll(array_merge(request()->query(), [
                'channel_id'           => core()->getCurrentChannel()->id,
                'status'               => 1,
                'visible_individually' => 1,
            ]));
        }

        
        
            
            
        // foreach($products as $product){
        //     dd($product->active_inventories);
        // }
            
        if (! empty(request()->query('query'))) {
            /**
             * Update or create search term only if
             * there is only one filter that is query param
             */
            if (count(request()->except(['mode', 'sort', 'limit'])) == 1) {
                UpdateCreateSearchTermJob::dispatch([
                    'term'       => request()->query('query'),
                    'results'    => $products->total(),
                    'channel_id' => core()->getCurrentChannel()->id,
                    'locale'     => app()->getLocale(),
                ]);
            }
        }

        return ProductResource::collection($products);
    }

    /**
     * Related product listings.
     *
     * @param  int  $id
     */
    public function relatedProducts($id): JsonResource
    {
        $product = $this->productRepository->findOrFail($id);
       
        $relatedProducts = $product->related_products()
            ->take(core()->getConfigData('catalog.products.product_view_page.no_of_related_products'))
            ->get();

        return ProductResource::collection($relatedProducts);
    }

    /**
     * Up-sell product listings.
     *
     * @param  int  $id
     */
    public function upSellProducts($id): JsonResource
    {
        $product = $this->productRepository->findOrFail($id);
       
        $upSellProducts = $product->up_sells()
            ->take(core()->getConfigData('catalog.products.product_view_page.no_of_up_sells_products'))
            ->get();

        return ProductResource::collection($upSellProducts);
    }
}
