<?php

namespace Modules\Product\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\Canteen\Services\CanteenService;
use Modules\Category\Services\CategoryService;
use Modules\Product\Enums\ProductStatusEnum;
use Modules\Product\Requests\Admin\ProductRequest;
use Modules\Product\Services\ProductService;
use Modules\Currency\Services\CurrencyService;

class ProductController extends Controller
{

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

    public function __construct(
        private ProductService $productService,
        private CanteenService $canteenService,
        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]);
        }
        $canteens = $this->canteenService->get();
        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());
    }

    /**
     * Show the form for creating a new Product.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('products.store');
        $head= __('products.new');
        $canteens = $this->canteenService->get();
        $categories = [];
        $statuses = ProductStatusEnum::values();
        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
    {
        $this->productService->create($request->validated());

        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('products.index').'?valid='.$request->status,
        ];
        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('products.update', $product->id);
        $statuses = ProductStatusEnum::values();
        $canteens = $this->canteenService->get();
        $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);
        }
        $this->productService->update($request->validated(), $id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('products.index').'?valid='.$request->status,
        ];
        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('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('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('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('products.index'),
        ];
        return ResponseService::response($response);
    }
}
