<?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\Attribute\Services\AttributeService;
use Modules\Option\Services\OptionService;
use Modules\Product\Requests\Admin\ProductWarhouseRequest;
use Modules\Product\Services\ProductService;
use Modules\Product\Services\ProductWarehouseService;
use Modules\Warehouse\Services\WarehouseService;

class ProductWarehouseController extends Controller
{

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

    public function __construct(
        private ProductService $productService,
        private AttributeService $attributeService,
        private OptionService $optionService,
        private ProductWarehouseService $productWarehouseService,
        private WarehouseService $warehouseService
    )
    {}

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

    /**
     * return data of the product_warehouses.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $request->merge(['paginate'=>true]);
        $product_warehouses= $this->productWarehouseService->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('product_warehouses.store');
        $head= __('product_warehouses.new');
        $warehouses = $this->warehouseService->get();
        $product = null;
        $options = [];

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

    /**
     * Store a newly created Product in storage.
     * @param ProductWarhouseRequest $request
     * @return JsonResponse
     */
    public function store(ProductWarhouseRequest $request) :JsonResponse
    {
        $attribute_id = $this->productService->getItem($request->product_id)->attribute_id;
        $data = $request->validated();
        $data['attribute_id']=$attribute_id;
        $this->productWarehouseService->create($data);

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

    public function options(Request $request)
    {
        $product = $this->productService->checkItem($request->product_id);
        if (!$product)
        {
            return abort(404);
        }
        $options = [];
        if ($product->attribute_id)
        {
            $options = $this->optionService->get(['attribute_id'=>$product->attribute_id]);
        }
        return view($this->viewDirectory.'options',get_defined_vars());
    }

    public function item(Request $request)
    {
        $key = $request->key;
        return view($this->viewDirectory.'product_item', get_defined_vars());
    }

    /**
     * Show the form for editing the specified Product.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $productWarehouse = $this->productWarehouseService->getItem($id);
        if (!$productWarehouse)
        {
            abort(404);
        }
        $action= route('product_warehouses.update', $productWarehouse->id);
        $head= __('product_warehouses.edit');
        $warehouses = $this->warehouseService->get();
        $product = $productWarehouse->product;
        $options = $this->optionService->get(['attribute_id'=>$productWarehouse->product->attribute_id]);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified Product in storage.
     * @param ProductWarhouseRequest $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(ProductWarhouseRequest $request, int $id) :JsonResponse
    {
        $attribute_id = $this->productService->getItem($request->product_id)->attribute_id;
        $data = $request->validated();
        $data['attribute_id']=$attribute_id;
        $product = $this->productWarehouseService->checkItem($id);
        if (!$product)
        {
            abort(404);
        }
        $this->productWarehouseService->update($request->validated(), $id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('product_warehouses.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->productWarehouseService->destroy($id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('product_warehouses.index'),
        ];
        return ResponseService::response($response);
    }

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

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

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