<?php

namespace Modules\Region\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\Services\CountryService;
use Modules\Region\Exports\RegionExport;
use Modules\Region\Requests\Employee\RegionRequest;
use Modules\Region\Services\RegionService;
use Modules\Currency\Services\CurrencyService;
use Modules\MainService\Enums\ViewEnum;

class RegionController extends Controller
{

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

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

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

    /**
     * return data of the regions.
     *
     * @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)
        {
            $regions = $this->regionService->get($request->all());
            return Excel::download(new RegionExport($regions,Auth::user()), 'regions.xlsx');
        }
        $request->merge([
            'paginate' => true,
            'route' => $this->viewDirectory,
            'view' => ViewEnum::LIST->value,
            'request'=>$request,
            'countries'=>$this->countryService->get()
        ]);
        return $this->regionService->renderView($request->all());
    }

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

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

        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('employee.regions.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Show the form for editing the specified Region.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $region = $this->regionService->getItem($id);
        if (!$region)
        {
            abort(404);
        }
        $data =
            [
                'head'=>__('regions.new'),
                'action' => route('employee.regions.update',$region->id),
                'view' => ViewEnum::FORM->value,
                'route'=> $this->viewDirectory,
                'countries'=>$this->countryService->get(),
                'region' => $region
            ];
        return $this->regionService->render($data);
    }

    public function lock($id)
    {
        $region = $this->regionService->getItem($id);
        if (!$region)
        {
            abort(404);
        }
        $this->regionService->updateLock($id);
        session()->flash('message', __('dashboard.saveDone'));
        return redirect()->back();
    }

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

    /**
     * Update the specified region in storage.
     * @param RegionRequest $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(RegionRequest $request, int $id) :JsonResponse
    {
        $region = $this->regionService->checkItem($id);
        if (!$region)
        {
            abort(404);
        }
        $this->regionService->update($request->validated(), $id);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('employee.regions.index'),
        ];
        return ResponseService::response($response);
    }

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

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

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

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