<?php

namespace Modules\Product\Controllers\Store;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Modules\Attribute\Enums\AttributeStatusEnum;
use Modules\Attribute\Services\AttributeService;
use Modules\Category\Services\CategoryService;
use Modules\Option\Enums\OptionStatusEnum;
use Modules\Option\Services\OptionService;
use Modules\Product\Enums\ProductStatusEnum;
use Modules\Product\Models\Product;
use Modules\Product\Requests\Store\ProductRequest;
use Modules\Product\Services\ProductService;

class ProductController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/store/product';
    /** View folder */
    private $viewDirectory= 'store.product.';
    //
    private ProductService $productService;
    private CategoryService $categoryService;
    private AttributeService $attributeService;
    private OptionService $optionService;

    public function __construct(ProductService $productService , CategoryService $categoryService,AttributeService $attributeService ,OptionService $optionService)
    {
        $this->productService = $productService;
        $this->categoryService = $categoryService;
        $this->attributeService = $attributeService;
        $this->optionService = $optionService;
    }

    /**
     * Display a listing of the products.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $request = $request->merge(['product_id'=>'main','store_id'=>Auth::user()->store_id]);
        $products = $this->productService->get($request);
        $title= __('products.head');
        $request = $request->merge(['category_id'=>'main','store_id'=>Auth::user()->store_id]);
        $categories = $this->categoryService->get($request);
        $request = $request->merge(['category_id'=>'sub','store_id'=>Auth::user()->store_id]);
        $sub_categories = $this->categoryService->get($request);
        $statuses = ProductStatusEnum::values();
        return view($this->viewDirectory.'index', get_defined_vars());
    }

    public function addition(Request $request)
    {
        $key = $request->key;
        return view($this->viewDirectory.'addition', get_defined_vars());
    }
    public function item(Request $request)
    {
        $key = $request->key;
        $keyItem = $request->keyItem;
        return view($this->viewDirectory.'item', get_defined_vars());
    }
    public function productItem(Request $request)
    {
        $key = $request->key;
        return view($this->viewDirectory.'product_item', get_defined_vars());
    }
    public function itemKey(Request $request)
    {
        $key = $request->key;
        $keyItem = $request->keyItem;
        return view($this->viewDirectory.'item', get_defined_vars());
    }

    /**
     * return data of the products.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) //:View
    {
        $request->merge(['paginate'=> true,'store_id'=>Auth::user()->store_id]);
        $products= $this->productService->get($request);

        return view($this->viewDirectory.'grid',compact('products'));
    }

    public function image(Request $request)
    {
        return view($this->viewDirectory.'image');
    }

    /**
     * Show the form for creating a new Product.
     *
     * @return View
     */
    public function create(Request $request) :View
    {
        $action= route('product.store');
        $head= __('products.new');
        $request = $request->merge(['category_id'=>'main','store_id'=>Auth::user()->store_id]);
        $categories = $this->categoryService->get($request);
        $statuses = ProductStatusEnum::values();
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Store a newly created Product in storage.
     * @param ProductRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(ProductRequest $request) :JsonResponse
    {
        $request->merge(['store_id'=>Auth::user()->store_id,'status'=>ProductStatusEnum::PENDING->value]);
        $this->productService->save($request, null);

        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('product.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Show the form for editing the specified Product.
     *
     * @param  Product  $product
     * @return View
     */
    public function edit(Request $request,Product $product) :View
    {
        // dd($product->id);
        $action= route('product.update', $product->id);
        $product= $this->productService->getItem($product);
        $head= __('products.edit');

        $request = $request->merge(['category_id'=>'main','store_id'=>Auth::user()->store_id]);
        $categories = $this->categoryService->get($request);

        $request = $request->merge(['category_id'=>'sub','store_id'=>Auth::user()->store_id]);
        $sub_categories = $this->categoryService->get($request);
        $items = $this->productService->items($product);
        $additions = $this->productService->additions($product);

        $logs = $this->productService->logs($product);
        $request->merge(['type'=>OptionStatusEnum::REASON_REFUSE_PRODUCT->value]);
        $reasons = $this->optionService->get($request);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified Product in storage.
     * @param ProductRequest $request
     * @param Product $product
     * @return RedirectResponse|Redirector
     */
    public function update(ProductRequest $request, Product $product) :JsonResponse
    {
        $request->merge(['store_id'=>Auth::user()->store_id,'status'=>ProductStatusEnum::PENDING->value]);
        $this->productService->save($request, $product);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('product.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove the specified Product from storage.
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $return= $this->productService->destroy($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Remove several products by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $return= $this->productService->destroyAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore the specified Product from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $return= $this->productService->restore($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore several products by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $return= $this->productService->restoreAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }
}
