require_dependency "business_time_admin/application_controller"

module BusinessTimeAdmin
  class HolidaysController < ApplicationController
    before_action :set_holiday, only: [:show, :edit, :update, :destroy]

    # GET /holidays
    def index
      @holidays = Holiday.all
    end

    # GET /holidays/1
    def show
    end

    # GET /holidays/new
    def new
      @holiday = Holiday.new
    end

    # GET /holidays/1/edit
    def edit
    end

    # POST /holidays
    def create
      @holiday = Holiday.new(holiday_params)

      if @holiday.save
        #redirect_to holidays_url, notice: 'Holiday was successfully created.'
        redirect_to the_destination, notice: 'Holiday was successfully created.'
      else
        render :new
      end
    end

    # PATCH/PUT /holidays/1
    def update
      if @holiday.update(holiday_params)
        redirect_to the_destination, notice: 'Holiday was successfully updated.'
      else
        render :edit
      end
    end

    # DELETE /holidays/1
    def destroy
      @holiday.destroy
      redirect_to the_destination, notice: 'Holiday was successfully destroyed.'
    end

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

      # Only allow a trusted parameter "white list" through.
      def holiday_params
        params.require(:holiday).permit(:holiday_date)
      end

      def the_destination
        BusinessTimeAdmin.destination_for_successful_action.nil? ? holidays_path : BusinessTimeAdmin.destination_for_successful_action
      end
  end
end
