class ReservationsController < ApplicationController
  before_action :set_reservation, only: [:show, :edit, :update, :destroy]
  before_action :require_authorization!, only: [:edit, :update]
  before_action :require_login


  # GET /reservations
  # GET /reservations.json
  def index
    @reservations = Reservation.all
  end

  # GET /reservations/1
  # GET /reservations/1.json
  def show
  end

  # GET /reservations/new
  def new
    @reservation = Reservation.new
  end

  # GET /reservations/1/edit
  def edit
  end

  # POST /reservations
  # POST /reservations.json
  def create
    @kitchen = Kitchen.find(params[:kitchen_id])
    @reservation = @kitchen.reservations.build(reservation_params)
    @reservation.user = current_user

    if @reservation.save
      @reservation.send_new_reservation_email(@kitchen)
      redirect_to reservations_all_users_path, notice: 'Reservation was successfully sent.'
    else
      render :new
    end
  end

  # PATCH/PUT /reservations/1
  # PATCH/PUT /reservations/1.json
  def update
    if @reservation.update(reservation_params)
      redirect_to reservations_pending_kitchen_path(@reservation.kitchen), notice: 'Reservation was succesfully updated'
    else
      render :edit
    end
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_reservation
    @reservation = Reservation.find(params[:id])
  end

  def require_login
    redirect_to root_path unless logged_in?
  end

  def require_authorization!
    redirect_to root_path unless @reservation.editable_by?(current_user)
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def reservation_params
    params.require(:reservation).permit(:message_from_guest, :message_from_host, :reserve_date, :reserve_time, 
    :kitchen_id, :guest_number, :status)
  end
end
