<commit_before>require 'httparty'
class EntriesController < ApplicationController
  skip_before_action :authentication, only: [:sms_approve]
  before_action :authentication, only: [:create, :index, :pending, :approve]

  def create
    entry = Entry.create(bet_id: params[:bet_id])
    params[:users].each do |user|
      if user[1]["winner"] == "true"
        detail = entry.winners.create(winner_id: user[1]["user_id"].to_i)
      else
        detail = entry.losers.create(loser_id: user[1]["user_id"].to_i)
      end
    end
    entry.confirmations.create(approved: true, user_id: current_user.id)

    bet = Bet.find(params[:bet_id])
    users = bet.users.where.not(id: current_user.id)
    users.each do |user|
      twilio_approve_notification(user.phone, bet.id, bet.name)
    end

    render json: {message: "Entry created"}
  end

  def index
    entries = Entry.where(bet_id: params[:bet_id]).includes(:winners, :losers)
    render json: entries.to_json(
      include: [:winners, :losers]
      )
  end

  def pending
    bet = Bet.find(params[:bet_id])
    if bet.entries.count != 0
      if bet.entries.last.confirmations.count != bet.users.count
        if bet.entries.last.confirmations.where(user_id: current_user.id).count != 0
            pending = "approved"
            losers = []
            bet.entries.last.losers.each do |loser|
              losers << loser.loser.first_name
            end
            winners = []
            bet.entries.last.winners.each do |winner|
              winners << winner.winner.first_name
            end
        else
          pending = "true"
          losers = []
          bet.entries.last.losers.each do |loser|
            losers << loser.loser.first_name
          end
          winners = []
          bet.entries.last.winners.each do |winner|
            winners << winner.winner.first_name
          end
        end
      end
    else
      pending = "false"
    end
    render json: {pending: pending, losers: losers, winners: winners}
  end

  def approve
    bet = Bet.find(params[:bet_id])
    if bet.entries.last.confirmations.where(user_id: current_user.id).count == 0
      bet.entries.last.confirmations.create(user_id: current_user.id, approved: true)
    end
    create_venmo_transaction(bet)
    render json: {message: "approved"}
  end

  def sms_approve
    from_input = params[:From]
    from = from_input[2, from_input.length]
    body = params[:Body].upcase

    user = User.find_by(phone: from)
    p "before yes"
    if body[0,3] == "YES"
      bet_id = body[3, body.length]
      bet = user.bets.where(id: bet_id)[0]

      if bet.entries.last.confirmations.where(user_id: user.id).count == 0
        bet.entries.last.confirmations.create(user_id: user.id, approved: true)
      end
      create_venmo_transaction(bet)
    end
  end

  private

  def create_venmo_transaction(bet)
    if bet.entries.last.confirmations.count == bet.users.count

      winners_uids_arr = []
      bet.entries.last.winners.each do |winner|
        winners_uids_arr << winner.winner.uid
      end

      losers_arr = []
      bet.entries.last.losers.each do |loser|
        losers_arr << [loser.loser.token, loser.loser.phone]
      end

      winners_names_arr = []
      bet.entries.last.winners.each do |winner|
        winners_names_arr << winner.winner.first_name
      end
      losers_names_arr = []
      bet.entries.last.losers.each do |loser|
        losers_names_arr << loser.loser.first_name
      end

      winners = ''<commit_msg>finally<commit_after>require 'httparty'
class EntriesController < ApplicationController
  skip_before_action :authentication, only: [:sms_approve]
  before_action :authentication, only: [:create, :index, :pending, :approve]

  def create
    entry = Entry.create(bet_id: params[:bet_id])
    params[:users].each do |user|
      if user[1]["winner"] == "true"
        detail = entry.winners.create(winner_id: user[1]["user_id"].to_i)
      else
        detail = entry.losers.create(loser_id: user[1]["user_id"].to_i)
      end
    end
    entry.confirmations.create(approved: true, user_id: current_user.id)

    bet = Bet.find(params[:bet_id])
    users = bet.users.where.not(id: current_user.id)
    users.each do |user|
      twilio_approve_notification(user.phone, bet.id, bet.name)
    end

    render json: {message: "Entry created"}
  end

  def index
    entries = Entry.where(bet_id: params[:bet_id]).includes(:winners, :losers)
    render json: entries.to_json(
      include: [:winners, :losers]
      )
  end

  def pending
    bet = Bet.find(params[:bet_id])
    if bet.entries.count != 0
      if bet.entries.last.confirmations.count != bet.users.count
        if bet.entries.last.confirmations.where(user_id: current_user.id).count != 0
            pending = "approved"
            losers = []
            bet.entries.last.losers.each do |loser|
              losers << loser.loser.first_name
            end
            winners = []
            bet.entries.last.winners.each do |winner|
              winners << winner.winner.first_name
            end
        else
          pending = "true"
          losers = []
          bet.entries.last.losers.each do |loser|
            losers << loser.loser.first_name
          end
          winners = []
          bet.entries.last.winners.each do |winner|
            winners << winner.winner.first_name
          end
        end
      end
    else
      pending = "false"
    end
    render json: {pending: pending, losers: losers, winners: winners}
  end

  def approve
    bet = Bet.find(params[:bet_id])
    if bet.entries.last.confirmations.where(user_id: current_user.id).count == 0
      bet.entries.last.confirmations.create(user_id: current_user.id, approved: true)
    end
    create_venmo_transaction(bet)
    render json: {message: "approved"}
  end

  def sms_approve
    from_input = params[:From]
    from = from_input[2, from_input.length]
    body = params[:Body].upcase

    user = User.find_by(phone: from)
    p "before yes"
    if body[0,3] == "YES"
      bet_id = body[3, body.length]
      bet = user.bets.where(id: bet_id)[0]

      if bet.entries.last.confirmations.where(user_id: user.id).count == 0
        bet.entries.last.confirmations.create(user_id: user.id, approved: true)
        create_venmo_transaction(bet)
      end

    end
  end

  private

  def create_venmo_transaction(bet)
    if bet.entries.last.confirmations.count == bet.users.count

      winners_uids_arr = []
      bet.entries.last.winners.each do |winner|
        winners_uids_arr << winner.winner.uid
      end

      losers_arr = []
      bet.entries.last.losers.each do |loser|
        losers_arr << [loser.loser.token, loser.loser.phone]
      end

      winners_names_arr = []
      bet.entries.last.winners.each do |winner|
        winners_names_arr << winner.winner.first_name
      end
      losers_names_arr = []
      bet.entries.last.losers.each do |loser|
        losers_names_arr << loser.loser.first_name
      end

      winners = ''
{'fraction_non_alphanumeric': 0.11571896822003705, 'fraction_numerical': 0.003420265070542967, 'mean_word_length': 2.4605522682445757, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3207576', 'n_tokens_mistral': 2343, 'n_tokens_neox': 2303, 'n_words': 448}