<?php

namespace App\Http\Controllers;

use App\WashingMachine;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

use App\WashingReservation;
use Illuminate\View\View;

class WashingReservationController extends Controller
{
    public function __construct()
    {
        $this->middleware([ "auth" ]);

        $this->middleware([ "check.auth:washing.machine.reservation.list" ])->only("index");
        $this->middleware([ "check.auth:washing.machine.reservation.show" ])->only("show");
        $this->middleware([ "check.auth:washing.machine.reservation.create" ])->only("create", "store");
        $this->middleware([ "check.auth:washing.machine.reservation.edit" ])->only("edit", "update");
        $this->middleware([ "check.auth:washing.machine.reservation.delete" ])->only("delete");
    }

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return Application|Factory|View
     */
    public function index(Request $request)
    {
        $reservations = WashingReservation::query()->paginate($request->query("limit", 20));

        return Response::detect("washing-reservations.index", [ "reservations" => $reservations]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Application|Factory|View
     */
    public function create()
    {
        $machines = WashingMachine::all();
        return Response::detect("washing-reservations.create", [ 'machines' => $machines ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return Application|Factory|View
     */
    public function store(Request $request)
    {
        $data = $request->validate([
            "time" => "required",
            "machine" => "required"
        ]);

        $machineReservation = new WashingReservation($data);
        $machineReservation->save();

        $saved = $machineReservation->save();

        if(!$saved){
            return Response::detect("washing-reservations.store", [
                "washing_reservation" => $machineReservation
            ]);
        }else{
            $reservations = WashingReservation::query()->paginate($request->input("limit", 20));

            return Response::detect("washing-reservations.index", [ "reservations" => $reservations]);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Application|Factory|View
     */
    public function show($id)
    {
        $machineReservation = WashingReservation::find($id);

        return Response::detect("washing-reservations.show", [
            "machineReservation" => $machineReservation
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Application|Factory|View
     */
    public function edit($id)
    {
        $reservation = WashingReservation::query()->find($id);
        $machines = WashingMachine::all();

        return Response::detect("washing-reservations.edit", ['washing_reservation' => $reservation, 'machines' => $machines ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return Application|Factory|View
     */
    public function update(Request $request, $id)
    {
        $data = $request->validate([
            "time" => "required",
            "machine" => "required"
        ]);

        $machineReservation = WashingReservation::find($id);

        $machineReservation->update($data);

        $saved = $machineReservation->save();

        if(!$saved){
            return Response::detect("washing-reservations.update", [
                "washing_reservation" => $machineReservation
            ]);
        }else{
            $reservations = WashingReservation::query()->paginate($request->query("limit", 20));

            return Response::detect("washing-reservations.index", [ "reservations" => $reservations]);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return RedirectResponse
     */
    public function destroy($id)
    {
        $machineReservation = WashingReservation::find($id);
        $machineReservation->delete();

        $reservations = WashingReservation::query()->paginate( 20);

        return redirect()->route("washing-reservations.index", [ "reservations" => $reservations]);
    }
}