<?php

namespace Modules\Product\Controllers\Employee;

/** 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 App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
use Modules\Product\Exports\ProductExport;
use Modules\Product\Requests\Employee\ProductRequest;
use Modules\Product\Services\ProductService;
use Modules\MainService\Enums\ViewEnum;

class ProductController extends Controller
{

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

    public function __construct(
        private ProductService $productService
    ){}

    /**
     * Display a listing of the products.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $typeView = $request->typeView ?? ViewEnum::LIST->value;
        $data =
        [
            'head'=>__('products.head'),
            'view' => ViewEnum::INDEX->value,
            'route'=>$this->viewDirectory,
            'typeView' => $typeView,
            'listView'=>ViewEnum::LIST->value,
            'request' => $request,
        ];
        return $this->productService->render($data);
    }

    /**
     * return data of the products.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request)
    {
        if(isset($request->orderBy))
        {
            $orderBy = json_decode($request->orderBy, true);
            $request->merge(['orderBy'=>['key'=>$orderBy['key'], 'value'=>$orderBy['value']]]);
        }
        if($request->export)
        {
            $products = $this->productService->get($request->all());
            return Excel::download(new ProductExport($products,Auth::user()), 'products.xlsx');
        }
        $typeView = $request->typeView ?? ViewEnum::LIST->value;
        $request->merge([
            'route' => $this->viewDirectory,
            'view' => $typeView,
            'request'=>$request,
            'groupSector' => $typeView == ViewEnum::KANBAN->value ? true : false,
            'paginate' => $typeView != ViewEnum::KANBAN->value ? true : false,
        ]);
        return $this->productService->renderView($request->all());
    }

    /**
     * Show the form for creating a new product.
     *
     * @return View
     */
    public function create() :View
    {
        $data =
        [
            'head'=>__('products.new'),
            'action' => route($this->actionDirectory.'store'),
            'view' => ViewEnum::FORM->value,
            'route'=> $this->viewDirectory,
        ];
        return $this->productService->render($data);
    }

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

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

    /**
     * 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);
        }
        $data =
        [
            'head'=>__('products.edit'),
            'action' => route($this->actionDirectory.'update',$product->id),
            'view' => ViewEnum::FORM->value,
            'route'=> $this->viewDirectory,
            'product' => $product
        ];
        return $this->productService->render($data);
    }

    public function show(int $id) :View
    {
        $product = $this->productService->getItem($id);
        if (!$product)
        {
            abort(404);
        }
        $data =
            [
                'head'=>__('products.show'),
                'view' => ViewEnum::SHOW->value,
                'route'=> $this->viewDirectory,
                'product' => $product
            ];
        return $this->productService->render($data);
    }

    /**
     * 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->actionDirectory.'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->actionDirectory.'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->actionDirectory.'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->actionDirectory.'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->actionDirectory.'index'),
        ];
        return ResponseService::response($response);
    }
}
