<?php

namespace App\Http\Controllers\Admin;

/** Ajax Request */

use App\Http\Requests\Admin\ReservationRequest;
use App\Models\Reservation;
use App\Models\ReservationPayment;
use App\Services\Interfaces\ReservationRepositoryInterface;
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 Maatwebsite\Excel\Facades\Excel;

class ReservationController extends Controller
{

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

    public function __construct(ReservationRepositoryInterface $reservationRepository)
    {
        $this->reservationRepository = $reservationRepository;
    }

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

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

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

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

    /**
     * Store a newly created Reservation in storage.
     * @param Request $request
     * @return RedirectResponse|Redirector
     */
    public function store(Request $request) :JsonResponse
    {


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

    /**
     * Show the form for editing the specified Reservation.
     *
     * @param  Reservation  $reservation
     * @return View
     */
    public function edit(Reservation $reservation) :View
    {
        $action= route('reservations.update', $reservation->id);
        $payment = ReservationPayment::where('reservation_id',$reservation->id)->first();
        $head= __('reservations.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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