<?php

namespace Modules\Country\Controllers\Employee;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
use Modules\Country\Exports\CountryExport;
use Modules\Country\Requests\Employee\CountryRequest;
use Modules\Country\Services\CountryService;
use Modules\MainService\Enums\ViewEnum;

class CountryController extends Controller
{

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

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

    /**
     * Display a listing of the countries.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $data =
            [
                'head'=>__('countries.head'),
                'view' => ViewEnum::INDEX->value,
                'route'=>$this->viewDirectory,
                'request' => $request,
            ];
        return $this->countryService->render($data);
    }

    /**
     * return data of the countries.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request)
    {
        if(isset($request->orderBy))
        {
            $orderBy = json_decode($request->orderBy, true);
            $request->merge(['orderBy'=>['key'=>$orderBy['key'], 'value'=>$orderBy['value']]]);
        }
        if($request->export)
        {
            $countries = $this->countryService->get($request->all());
            return Excel::download(new CountryExport($countries,Auth::user()), 'countries.xlsx');
        }
        $request->merge([
            'paginate' => true,
            'route' => $this->viewDirectory,
            'view' => ViewEnum::LIST->value,
            'request'=>$request,
        ]);
        return $this->countryService->renderView($request->all());
    }

    /**
     * Show the form for creating a new Country.
     *
     * @return View
     */
    public function create() :View
    {
        $data =
            [
                'head'=>__('countries.new'),
                'action' => route($this->actionDirectory.'store'),
                'view' => ViewEnum::FORM->value,
                'route'=> $this->actionDirectory,
            ];
        return $this->countryService->render($data);
    }

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

        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route($this->actionDirectory.'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);
        }
        $data =
            [
                'head'=>__('countries.new'),
                'action' => route($this->actionDirectory.'update',$country->id),
                'view' => ViewEnum::FORM->value,
                'route'=> $this->actionDirectory,
                'country' => $country
            ];
        return $this->countryService->render($data);
    }

    public function show(int $id) :View
    {
        $country = $this->countryService->getItem($id);
        if (!$country)
        {
            abort(404);
        }
        $data =
            [
                'head'=>__('countries.show'),
                'view' => ViewEnum::SHOW->value,
                'route'=> $this->viewDirectory,
                'country' => $country
            ];
        return $this->countryService->render($data);
    }

    /**
     * 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);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route($this->actionDirectory.'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($this->actionDirectory.'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($this->actionDirectory.'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($this->actionDirectory.'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($this->actionDirectory.'index'),
        ];
        return ResponseService::response($response);
    }
}
