<?php

namespace Sarga\API\Http\Controllers;

use Sarga\API\Http\Resources\Catalog\Brand;
use Sarga\API\Http\Resources\Catalog\Product as ProductResource;
use Sarga\API\Http\Resources\Core\Source;
use Sarga\API\Http\Resources\Core\Vendor;
use Sarga\API\Http\Resources\Core\Review;
use Sarga\API\Http\Resources\Core\VendorDetail;
use Sarga\Shop\Repositories\ProductRepository;
use Sarga\Brand\Repositories\BrandRepository;
use Sarga\Shop\Repositories\CategoryRepository;
use Sarga\Shop\Models\Category;
use Webkul\Marketplace\Repositories\ReviewRepository;
use Sarga\Shop\Repositories\VendorRepository;
use Webkul\RestApi\Http\Controllers\V1\V1Controller;

class Vendors extends V1Controller
{

    public function __construct(protected VendorRepository $vendorRepository,
                                protected CategoryRepository $categoryRepository,
                                protected ReviewRepository $reviewRepository
                                )
    {
    }

    public function sources(){
        $vendors = $this->vendorRepository->select('marketplace_sellers.id','url','shop_title')
            ->where('is_approved',true)
            ->with(['categories:seller_id,type,categories'])
//            ->leftJoin('seller_categories','marketplace_sellers.id','=','seller_categories.seller_id')
            ->get();
//return $vendors;

        if(! $vendors){
            return response()->json(['error' => 'not found'],404);
        }

//        return $vendors->first()->categories()->first();
//        return  json_decode($cats->categories,true);
        $categorizedVendors = $vendors->map(function ($item, $key){
            if($item->categories && $mainCats = $item->categories()->first()){
                $cat_ids = json_decode($mainCats->categories,true);
//                $vendor->test = Category::collection($this->categoryRepository->getVisibleCategoryTree($cat_ids[0]));
                $item->main_categories = $this->categoryRepository->whereIn('id',$cat_ids)
                    ->select('id','image','position','parent_id','display_mode','category_icon_path')
                    ->where('status',1)
                    ->with(['children'=> function($q){
                        $q->orderBy('position','asc');
                    }])
                    ->orderBy('position','asc')
                    ->get();

            }
            return $item;
        });


        return Source::collection($categorizedVendors);
    }

    public function index()
    {
        $vendors = $this->vendorRepository->select('marketplace_sellers.id','url','logo','banner','shop_title','ship_time','ship_price','slogan')
            ->where('is_approved',true)
            ->with(['categories:seller_id,categories'])
//            ->leftJoin('seller_categories','marketplace_sellers.id','=','seller_categories.seller_id')
            ->get();
        $reviewed_vendors = $vendors->map(function ($item, $key){
            $item->review_average = $this->reviewRepository->getAverageRating($item);
            return $item;
        });

        return Vendor::collection($reviewed_vendors);
    }

    public function products(ProductRepository $productRepository,$seller_id){
        $products = $productRepository->findAllBySeller($seller_id,request()->input('category_id'));

        return ProductResource::collection($products);
    }

    public function sellerProducts(ProductRepository $productRepository,$seller_id){
        $products = $productRepository->findAllBySellerNotActive($seller_id,request()->input('category_id'));

        return ProductResource::collection($products);
    }

    public function sellerProductsById(ProductRepository $productRepository,$seller_id, $product_id){
        $products = $productRepository->findAllBySellerNotActive($seller_id,request()->input('category_id'), $product_id);

        return ProductResource::collection($products);
    }

    public function vendor($seller_id){
        $vendor = $this->vendorRepository->select('marketplace_sellers.id','url','logo','banner','shop_title','ship_time','ship_price','slogan')
        ->where('id',$seller_id)
        ->with(['categories:seller_id,categories'])
        ->first();

        if($vendor->categories && $mainCats = $vendor->categories()->first()){
            $cat_ids = json_decode($mainCats->categories,true);
//                $vendor->test = Category::collection($this->categoryRepository->getVisibleCategoryTree($cat_ids[0]));
            $vendor->main_categories = $this->categoryRepository->whereIn('id',$cat_ids)
                ->select('id','image','position','parent_id','display_mode','category_icon_path')
                ->where('status',1)
                ->with(['children'=> function($q){
                    $q->orderBy('position','asc');
                }])
                ->orderBy('position','asc')
                ->get();

        }
        // dd($vendor->categories()->first());

        $vendor->review_average = $this->reviewRepository->getAverageRating($vendor);

        // $vendor->reviews = $this->reviewRepository
        // ->where('marketplace_seller_id', $seller_id)
        // ->where('status', 'approved')
        // ->get();

        return VendorDetail::make($vendor);
    }

    // public function getReviews($seller_id){
    //     $reviews = $this->reviewRepository->where('status', 'approved')->where('marketplace_seller_id', $seller_id)->paginate();
    //     return Review::collection($reviews);
    // }

    public function brands(BrandRepository $brandRepository, $seller_id){

        return Brand::collection($brandRepository->findAllBySeller($seller_id));
    }
}