<?php

namespace Modules\DealStatus\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\DealStatus\Requests\Admin\DealStatusRequest;
use Modules\DealStatus\Services\DealStatusService;
use Modules\MainService\Enums\ViewEnum;
use Modules\Sector\Services\SectorService;

class DealStatusController extends Controller
{

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

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

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

    /**
     * return data of the deal_statuses.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $request->merge([
            'paginate' => true,
            'route' => $this->viewDirectory,
            'view' => ViewEnum::LIST->value,
            'request'=>$request,
            'sectors'=>$this->sectorService->get()
        ]);
        return $this->dealStatusService->renderView($request->all());
    }

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

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

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

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

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

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

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

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

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