<?php

namespace Modules\City\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\Enums\CityBoolEnum;
use Modules\City\Requests\Admin\CityRequest;
use Modules\City\Services\CityService;
use Modules\Country\Services\CountryService;
use Modules\Region\Services\RegionService;

class CityController extends Controller
{

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

    public function __construct(
        private CityService $cityService,
        private CountryService $countryService,
        private RegionService $regionService,
        private AreaService $areaService,
    )
    {}

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

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

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

    public function regions(Request $request)
    {
        $regions = $this->regionService->get(['country_id'=>$request->country_id]);
        return view($this->viewDirectory.'regions',get_defined_vars());
    }
    public function cities(Request $request)
    {
        $cities = $this->cityService->get(['region_id'=>$request->region_id]);
        return view($this->viewDirectory.'cities',get_defined_vars());
    }
    public function areas(Request $request)
    {
        $areas = $this->areaService->get(['city_id'=>$request->city_id]);
        return view($this->viewDirectory.'areas',get_defined_vars());
    }

    /**
     * Show the form for creating a new City.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('cities.store');
        $head= __('cities.new');
        $countries = $this->countryService->get([]);
        $regions = [];
        $statuses = CityBoolEnum::values();
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

    /**
     * Show the form for editing the specified City.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $city = $this->cityService->getItem($id);
        if (!$city)
        {
            abort(404);
        }
        $action= route('cities.update', $city->id);
        $head= __('cities.edit');
        $countries = $this->countryService->get([]);
        $regions = $this->regionService->get(['country_id'=>$city->region->country_id]);
        $statuses = CityBoolEnum::values();
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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