<?php

namespace Modules\Product\Controllers\Warehouse;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Modules\Attribute\Services\AttributeService;
use Modules\Brand\Services\BrandService;
use Modules\Category\Services\CategoryService;
use Modules\Option\Services\OptionService;
use Modules\Product\Enums\ProductStatusEnum;
use Modules\Product\Requests\Warehouse\ProductRequest;
use Modules\Product\Services\ProductService;
use Modules\Store\Services\StoreService;
use Modules\Type\Services\TypeService;

class ProductController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/warehouse/products';
    /** View folder */
    private $viewDirectory= 'warehouse.products.';
    //

    public function __construct(
        private ProductService $productService,
        private CategoryService $categoryService,
        private StoreService $storeService,
        private BrandService $brandService,
        private TypeService $typeService,
        private AttributeService $attributeService,
        private OptionService $optionService,
    )
    {}

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

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

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

    public function search(Request $request)
    {
        $products = $this->productService->get($request->all());
        return json_encode($products);
    }

    /**
     * Show the form for creating a new Product.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route($this->viewDirectory.'store');
        $head= __('products.new');
        $categories = $this->categoryService->get(['category_id'=>'main']);
        $sub_categories = [];
        $stores = $this->storeService->get();
        $brands = $this->brandService->get();
        $types = $this->typeService->get();
        $attributes = $this->attributeService->get();
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }


    /**
     * Store a newly created Product in storage.
     * @param ProductRequest $request
     * @return JsonResponse
     */
    public function store(ProductRequest $request) :JsonResponse
    {
        $data = $request->validated();
        $this->productService->create($data);

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

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

    /**
     * Show the form for editing the specified Product.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $product = $this->productService->getItem($id);
        if (!$product)
        {
            abort(404);
        }
        $action= route($this->viewDirectory.'update', $product->id);
        $statuses = ProductStatusEnum::values();
        $categories = $this->categoryService->get(['category_id'=>'main']);
        $sub_categories = $this->categoryService->get(['category_id'=>$product->category_id]);
        $brands = $this->brandService->get();
        $stores = $this->storeService->get();
        $types = $this->typeService->get();
        $attributes = $this->attributeService->get();
        $items = $this->productService->items($id);
        $head= __('products.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified Product in storage.
     * @param ProductRequest $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(ProductRequest $request, int $id) :JsonResponse
    {
        $product = $this->productService->checkItem($id);
        if (!$product)
        {
            abort(404);
        }
        $this->productService->update($request->validated(), $id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route($this->viewDirectory.'index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove the specified Product from storage.
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(int $id) :JsonResponse
    {
        $this->productService->destroy($id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route($this->viewDirectory.'index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove several products by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $this->productService->destroyAll([
            'ids' => $request->ids,
            'status' => 1
        ]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route($this->viewDirectory.'index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Restore the specified Product from storage.
     *
     * @param int $id
     * @return JsonResponse
     */
    public function restore(int $id) :JsonResponse
    {
        $this->productService->destroy($id);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route($this->viewDirectory.'index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Restore several products by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $this->productService->destroyAll([
            'ids' => $request->ids,
            'status' => 0
        ]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route($this->viewDirectory.'index'),
        ];
        return ResponseService::response($response);
    }
}
