# frozen_string_literal: true
module Admin
  class VoteController < AdminController
    def up
      @candidate = Candidate.find_by(test_hash: params[:test_hash])
      authorize ReviewerVote.find_by(user_id: current_user.id, candidate_id: @candidate.id)
      current_user.cast_yea_on(@candidate)
    end

    def down
      @candidate = Candidate.find_by(test_hash: params[:test_hash])
      authorize ReviewerVote.find_by(user_id: current_user.id, candidate_id: @candidate.id)
      current_user.cast_nay_on(@candidate)
    end

    def interview_request
      @candidate = Candidate.find_by(test_hash: params[:test_hash])
      authorize ReviewerVote.find_by(user_id: current_user.id, candidate_id: @candidate.id)

      if interview_params[:review_comments].blank?
        refuse_interview_request
      else
        send_interview_request
      end
    end

    private

      def refuse_interview_request
        redirect_to admin_result_path(@candidate.test_hash),
                    flash: { error: "Must provide a comment" }
      end

      def send_interview_request
        current_user.review_candidate(@candidate, interview_params)
        RecruiterMailer.candidate_reviewed(@candidate).deliver_later
        redirect_to admin_result_path(@candidate.test_hash),
                    flash: { notice: "Quiz #{interview_params[:review_status]}" }
      end

      def interview_params
        params.permit(:review_status, :review_comments)
      end
  end
end
