<?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\City\Services\CityService;
use Modules\Country\Services\CountryService;
use Modules\Region\Services\RegionService;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Services\UserService;
use Modules\Warehouse\Requests\Admin\WarehouseRequest;
use Modules\Warehouse\Services\WarehouseService;

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 CountryService $countryService,
        private RegionService $regionService,
        private CityService $cityService,
        private UserService $userService
    )
    {}

    /**
     * Display a listing of the warehouses.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('warehouses.head');
        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');
        $countries = $this->countryService->get();
        $regions = [];
        $cities = [];
        $city_ids = [];
        $region_ids = [];
        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
    {
        $warehouse = $this->warehouseService->create($request->validated());
        $content =
            [
                'name'=>$request->title_ar,
                'email'=>$request->email,
                'phone'=>$request->phone,
                'password'=>$request->phone,
                'role'=>UserRoleEnum::WAREHOUSE->value,
                'warehouse_id'=>$warehouse->id
            ];
        $this->userService->create($content);
        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');
        $countries = $this->countryService->get();
        $regions = $this->regionService->get(['country_id'=>$warehouse->country_id]);
        $city_ids = $this->warehouseService->citiesId($id);
        $region_ids = $this->warehouseService->regionsId($id);
        $cities = $this->cityService->get(['region_id'=>$region_ids]);

        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);

        $user = $this->userService->get([
            'role'=>UserRoleEnum::WAREHOUSE->value,
            'warehouse_id'=>$id
        ])->first();
        $content =
            [
                'name'=>$request->title_ar,
                'email'=>$request->email,
                'phone'=>$request->phone,
                'password'=>$request->password,
                'role'=>UserRoleEnum::WAREHOUSE->value,
                'warehouse_id'=>$id
            ];
        if ($user)
        {
            $this->userService->update($content,$user->id);
        }
        else
        {
            $this->userService->create($content);
        }

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

    public function regions(Request $request)
    {
        $regions = $this->regionService->get($request->all());
        return view($this->viewDirectory.'regions',get_defined_vars());
    }
    public function cities(Request $request)
    {
        if (isset($request->region_id))
        {
            $region_ids = explode(',',$request->region_id) ;
            $cities = $this->cityService->get(['region_id'=>$region_ids]);
        }
        else
        {
            $cities = [];
        }
        $city_ids = explode(',',$request->city_id) ;
        return view($this->viewDirectory.'cities',get_defined_vars());
    }

    /**
     * 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);
    }
}
