<?php

namespace Modules\Country\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\Country\Requests\Admin\CountryRequest;
use Modules\Country\Services\CountryService;
use Modules\Currency\Services\CurrencyService;

class CountryController extends Controller
{

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

    public function __construct(
        private CountryService $countryService,
    )
    {}

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

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

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

    /**
     * Show the form for creating a new Country.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('countries.store');
        $head= __('countries.new');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

    /**
     * Show the form for editing the specified Country.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $country = $this->countryService->getItem($id);
        if (!$country)
        {
            abort(404);
        }
        $action= route('countries.update', $country->id);
        $head= __('countries.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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