<?php

namespace Modules\Rate\Controllers\Admin;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Modules\Rate\Enums\RateStatusEnum;
use Modules\Rate\Models\Rate;
use Modules\Rate\Requests\Admin\RateRequest;
use Modules\Rate\Services\RateService;

class RateController extends Controller
{

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

    public function __construct(RateService $rateService)
    {
        $this->rateService = $rateService;
    }

    /**
     * Display a listing of the rates.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('rates.head');
        $rates = [1,2,3,4,5];
        $statuses = RateStatusEnum::values();
        return view($this->viewDirectory.'index', get_defined_vars());
    }

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

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

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

    /**
     * Store a newly created Rate in storage.
     * @param RateRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(RateRequest $request) :JsonResponse
    {
        $rate= $this->rateService->save($request, null);

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

    /**
     * Show the form for editing the specified Rate.
     *
     * @param  Rate  $rate
     * @return View
     */
    public function edit(Rate $rate) :View
    {
        // dd($rate->id);
        $action= route('rates.update', $rate->id);
        $data= $this->rateService->getItem($rate);
        $head= __('rates.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified Rate in storage.
     * @param RateRequest $request
     * @param Rate $rate
     * @return RedirectResponse|Redirector
     */
    public function update(RateRequest $request, Rate $rate) :JsonResponse
    {
        $rate= $this->rateService->save($request, $rate);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('rates.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove the specified Rate from storage.
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $return= $this->rateService->destroy($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Remove several rates by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $return= $this->rateService->destroyAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore the specified Rate from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $return= $this->rateService->restore($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore several rates by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $return= $this->rateService->restoreAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }
}
