<?php

namespace Modules\Area\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\Requests\Admin\AreaRequest;
use Modules\Area\Services\AreaService;
use Modules\City\Services\CityService;
use Modules\Country\Services\CountryService;
use Modules\Region\Services\RegionService;

class AreaController extends Controller
{

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    /**
     * Get areas by city ID for AJAX requests
     *
     * @param int $cityId
     * @return JsonResponse
     */
    public function getByCity(int $cityId): JsonResponse
    {
        try {
            $areas = $this->areaService->getByCity($cityId);

            return ResponseService::response([
                'status' => 200,
                'message' => __('web.success'),
                'data' => $areas,
            ]);
        } catch (\Exception $e) {
            return ResponseService::response([
                'status' => 500,
                'message' => $e->getMessage(),
                'data' => [],
            ]);
        }
    }
}
