<?php

namespace Modules\WarehouseCategory\Controllers\Admin;

/** 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\WarehouseCategory\Requests\Admin\WarehouseCategoryRequest;
use Modules\WarehouseCategory\Services\WarehouseCategoryService;
use Modules\Currency\Services\CurrencyService;

class WarehouseCategoryController extends Controller
{

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

    public function __construct(
        private WarehouseCategoryService $productTypeService,
    )
    {}

    /**
     * Display a listing of the warehouse_categories.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('warehouse_categories.head');
        return view($this->viewDirectory.'index', get_defined_vars());
    }

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

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

    /**
     * Show the form for creating a new WarehouseCategory.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('warehouse_categories.store');
        $head= __('warehouse_categories.new');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

    /**
     * Show the form for editing the specified WarehouseCategory.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $warehouseCategory = $this->productTypeService->getItem($id);
        if (!$warehouseCategory)
        {
            abort(404);
        }
        $action= route('warehouse_categories.update', $warehouseCategory->id);
        $head= __('warehouse_categories.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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