<?php

namespace Modules\Sector\Controllers\Admin;

/** 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 Modules\MainService\Enums\ViewEnum;
use Modules\Sector\Exports\SectorExport;
use Modules\Sector\Requests\Admin\SectorRequest;
use Modules\Sector\Services\SectorService;
use Modules\Currency\Services\CurrencyService;
use Maatwebsite\Excel\Facades\Excel;

class SectorController extends Controller
{

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

    public function __construct(
        private SectorService $sectorService,
    )
    {}

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

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

        return $this->sectorService->renderView($request->all());
    }

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

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

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

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

    /**
     * Show the form for editing the specified Sector.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $sector = $this->sectorService->getItem($id);
        if (!$sector)
        {
            abort(404);
        }
        $data =
            [
                'head'=>__('sectors.edit'),
                'action' => route('sectors.update',$sector->id),
                'view' => ViewEnum::FORM->value,
                'route'=> $this->viewDirectory,
                'sector' => $sector
            ];
        return $this->sectorService->render($data);
    }

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

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

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

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

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

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