<?php

namespace Modules\Review\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\Review\Enums\ReviewRateEnum;
use Modules\Review\Models\Review;
use Modules\Review\Requests\Admin\ReviewRequest;
use Modules\Review\Services\ReviewService;

class ReviewController extends Controller
{

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

    public function __construct(ReviewService $reviewService)
    {
        $this->reviewService = $reviewService;
    }

    /**
     * Display a listing of the reviews.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('reviews.head');
        $rates = ReviewRateEnum::values();
        return view($this->viewDirectory.'index', get_defined_vars());
    }

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

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

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

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

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

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

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

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

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

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

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