<?php

namespace App\Http\Controllers\Web;

use App\Services\Response\ResponseService;
use Illuminate\Http\Request;
use Illuminate\View\View;
use App\Http\Controllers\Controller;
use Modules\Attribute\Enums\AttributeStatusEnum;
use Modules\Attribute\Services\AttributeService;
use Modules\Category\Models\Category;
use Modules\Category\Services\CategoryService;
use Modules\Product\Models\Product;
use Modules\Product\Models\ProductAttribute;
use Modules\Product\Services\ProductService;
use Modules\User\Services\AccountService;


class ProductController extends Controller
{
    private $view = 'web.pages.products.';
    private ProductService $productService ;
    private AccountService $accountService ;
    private CategoryService $categoryService ;
    private AttributeService $attributeService ;
    private int $pagination = 6 ;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(
        ProductService $productService,
        AccountService $accountService,
        CategoryService $categoryService,
        AttributeService $attributeService,
    )
    {
        $this->productService = $productService;
        $this->accountService = $accountService;
        $this->categoryService = $categoryService;
        $this->attributeService = $attributeService;
    }

    /**
     * Show the application dashboard.
     * @return View
     */
    public function products(Request $request)
    {
        $title = __('web.filter');
        $category_id = $request->category_id;
        if ($category_id)
        {
            $category = Category::findOrFail($category_id);
            $category = $this->categoryService->getItem($category);
        }
        $request->merge(['category_id'=>'main']);
        $categories = $this->categoryService->get($request);
        $request->merge(['type'=>AttributeStatusEnum::SIZE->value]);
        $sizes = $this->attributeService->get($request);
        $request->merge(['type'=>AttributeStatusEnum::COLOR->value]);
        $colors = $this->attributeService->get($request);
        return view($this->view.'products', get_defined_vars());
    }

    public function filter(Request $request)
    {
        $request->merge(['paginate'=>$this->pagination]);
        $productService = $this->productService->get($request);
        $isset_products = $productService->count();
        $products = $this->productService->get($request);
        $previous_page = $request->page -1 ;
        $next_page = $request->page + 1;
        return view($this->view.'products_cards', get_defined_vars());
    }

    public function fetchCategory(Request $request)
    {
        $categories = $this->categoryService->get($request);
        return view($this->view.'categories', get_defined_vars());
    }
    public function search(Request $request): View
    {
        $title = __('web.search');
        $request->merge(['title'=>$request->search,'paginate'=>$this->pagination]);
        $products = $this->productService->get($request);
        $params = 'search='.$request->search;

        return view($this->view.'search', get_defined_vars());
    }

    public function details($id)
    {
        $title = __('web.product_details');
        $product = Product::findOrFail($id);
        $product = $this->productService->getItem($product);
        $request = request()->merge(['paginate'=>$this->pagination,'category_id'=>$product->category_id,'not_id'=>$product->id]);
        $products = $this->productService->get($request);
        return view($this->view.'details', get_defined_vars());
    }

    public function favorite(Request $request)
    {
        $title = __('web.favorite');
        $request->merge(['favorite'=>true]);
        $products = $this->productService->get($request);

        return view($this->view.'favorites', get_defined_vars());
    }

    public function favoriteProduct(Request $request)
    {
        $account = $this->accountService->getAccount();
        $request->merge(['user_id'=>$account['user']->id,'status'=>$account['status']]);
        $this->productService->favorite($request);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> url()->previous(),
        ];
        return ResponseService::response($response);
    }

    public function fetchColors(Request $request)
    {
        $colors = ProductAttribute::where('main_attribute_id',$request->size_id)->where('product_id',$request->product_id)->get();
        $product = Product::find($request->product_id);
        return view($this->view.'colors', get_defined_vars());
    }
}
