<?php

namespace Modules\Warehouse\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\Area\Services\AreaService;
use Modules\City\Services\CityService;
use Modules\Region\Services\RegionService;
use Modules\Store\Services\StoreService;
use Modules\Warehouse\Enums\UpgradeWarehouseEnum;
use Modules\Warehouse\Requests\Admin\WarehouseRequest;
use Modules\Warehouse\Services\WarehouseService;
use Modules\Country\Services\CountryService;
use Modules\WarehouseCategory\Services\WarehouseCategoryService;

class WarehouseController extends Controller
{

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

    public function __construct(
        private WarehouseService $warehouseService,
        private WarehouseCategoryService $warehouseCategoryService,
        private StoreService $storeService,
        private CountryService $countryService,
        private CityService $cityService,
        private RegionService $regionService,
        private AreaService $areaService,
    )
    {}

    /**
     * Display a listing of the warehouses.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('warehouses.head');
        $warehoueCategories = $this->warehouseCategoryService->get([]);
        $warehouses = $this->warehouseService->get(['main'=>true]);
        $stores = $this->storeService->get([]);
        return view($this->viewDirectory.'index', get_defined_vars());
    }

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

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

    /**
     * Show the form for creating a new Warehouse.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('warehouses.store');
        $head= __('warehouses.new');
        $warehouseCategories = $this->warehouseCategoryService->get([]);
        $upgrade_statuses = UpgradeWarehouseEnum::values();
        $stores = $this->storeService->get([]);
        $countries = $this->countryService->get([]);
        $regions = [];
        $cities = [];
        $areas = [];
        $warehouses = $this->warehouseService->get(['warehouse_id'=>'main']);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

    /**
     * Show the form for editing the specified Warehouse.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $warehouse = $this->warehouseService->getItem($id);
        if (!$warehouse)
        {
            abort(404);
        }
        $action= route('warehouses.update', $warehouse->id);
        $head= __('warehouses.edit');
        $warehouseCategories = $this->warehouseCategoryService->get([]);
        $upgrade_statuses = UpgradeWarehouseEnum::values();
        $stores = $this->storeService->get([]);
        $countries = $this->countryService->get([]);
        $regions = $this->regionService->get(['country_id'=>$warehouse->country_id]);
        $cities = $this->regionService->get(['region_id'=>$warehouse->region_id]);
        $areas = $this->regionService->get(['city_id'=>$warehouse->city_id]);
        $warehouses = $this->warehouseService->get(['warehouse_id'=>'main']);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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