# 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)

      results = {
        message: "Vote Counted",
        upCount: @candidate.votes.yea.count,
        downCount: @candidate.votes.nay.count,
        myVote: "yea"
      }
      render json: results.to_json
    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)

      results = {
        message: "Vote Counted",
        upCount: @candidate.votes.yea.count,
        downCount: @candidate.votes.nay.count,
        myVote: "nay"
      }
      render json: results.to_json
    end

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

      results = {
        message: "Interview requested!",
        requestCopy: "Requested",
        declineCopy: "Decline Interview"
      }
      render json: results.to_json
    end

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

      results = {
        message: "Interview declined.",
        requestCopy: "Request Interview",
        declineCopy: "Declined"
      }
      render json: results.to_json
    end
  end
end
