<?php

namespace Modules\User\Controllers\Warehouse;

/** 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\Requests\Warehouse\ProfileRequest;
use Modules\User\Services\UserService;
use Modules\Warehouse\Services\WarehouseService;

class ProfileController extends Controller
{

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

    public function __construct(
        private UserService $userService,
        private CountryService $countryService,
        private WarehouseService $warehouseService,
        private CityService $cityService,
        private RegionService $regionService,
    )
    {}

    /**
     * Display a listing of the members.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $id = auth()->user()->warehouse_id;
        $warehouse = $this->warehouseService->getItem($id);
        if (!$warehouse)
        {
            abort(404);
        }
        $action = route('warehouse.profile.store');
        $head = __('profile.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.'index', get_defined_vars());
    }
    public function store(ProfileRequest $request) :JsonResponse
    {
        $id = auth()->user()->warehouse_id;
        $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($this->viewDirectory.'index'),
        ];
        return ResponseService::response($response);
    }
}
