<?php

namespace Modules\Product\Controllers\Canteen;

/** 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\Category\Services\CategoryService;
use Modules\Product\Enums\ProductStatusEnum;
use Modules\Product\Requests\Canteen\ProductRequest;
use Modules\Product\Services\ProductService;

class ProductController extends Controller
{

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

    public function __construct(
        private ProductService $productService,
        private CategoryService $categoryService,
    )
    {}

    /**
     * Display a listing of the products.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('products.head');
        if (is_null($request->valid) )
        {
            $request->merge(['valid'=>ProductStatusEnum::ACCEPT->value]);
        }
        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,'canteen_id'=>auth()->user()->canteen_id]);
        $products= $this->productService->get($request->all());

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

    /**
     * Show the form for creating a new Product.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('canteen.products.store');
        $head= __('products.new');
        $categories = $this->categoryService->get(['canteen_id'=>auth()->user()->canteen_id]);
        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();
        $data['canteen_id'] = auth()->user()->canteen_id;
        $this->productService->create($data);

        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('canteen.products.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);
        }
        $action= route('canteen.products.update', $product->id);

        $categories = $this->categoryService->get(['canteen_id'=>$product->canteen_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);
        }
        $data = $request->validated();
        $data['canteen_id'] = auth()->user()->canteen_id;
        $this->productService->update($data, $id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('canteen.products.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('canteen.products.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('canteen.products.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('canteen.products.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('canteen.products.index'),
        ];
        return ResponseService::response($response);
    }
}
