<?php

namespace Modules\ProductType\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\ProductType\Requests\Admin\ProductTypeRequest;
use Modules\ProductType\Services\ProductTypeService;
use Modules\Currency\Services\CurrencyService;

class ProductTypeController extends Controller
{

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

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

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

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

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

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

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

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

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

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

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

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

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

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