module KelpChallengeHelper
  def teamify team
    Team.find(team[0])
  end

  def get_team_avatar_from_team_id team_id
    team_obj = teamify(team_id)
    if team_obj.avatar_file_name.present?
      image_tag(team_obj.avatar.url(:square), class: "kelp_avatar_team", alt: "#{team_obj.name}, a Climate Cents team of supporters fighting climate change locally, avatar image")
    else
      image_tag("https://s3.amazonaws.com/climate-cents/kelp-challenge-image/kelp_TeamAvatar_fpo.jpg", class: "kelp_avatar_team", alt: 'The default avatar for a team not really into personalization for the Climate Cents Kelp Challenge')
    end
  end

  def get_company_name_from_team_id team_id
    team_obj = teamify(team_id)
    company_name = Team.find(team_obj).company_name
    company_name.nil? ? ' ' : company_name
  end

  def get_captain_from_team_id team_id
    team_obj = teamify(team_id)
    User.where(team_id: team_obj.id).first.name
  end

  def get_blurb_from_team_id team_id
    teamify(team_id).blurb
  end

  def get_members_from_team_id team_id
    team_obj = teamify(team_id)
    members = User.where(team_id: team_obj.id)
    members.length
  end

  def get_donors_from_team_id team_id
    team_obj = teamify(team_id)
    donors = Donation.select('DISTINCT stripe_email').where(team_id: team_obj.id)
    donors.length
  end

  def total_challenge_donated campaign_id
    number_to_currency(Donation.where(campaign_id: campaign_id).sum(:campaign_donation_in_cents)/100, precision: 0)
  end

  def total_challenge_donations campaign_id
    Donation.where(campaign_id: campaign_id).count
  end

  def total_challengers campaign_id
    teams = Team.where(campaign_id: campaign_id).map(&:id)
    User.where(:team_id => teams).count
  end

  def total_dives_funded campaign_id
    Donation.where(campaign_id: campaign_id).sum(:campaign_donation_in_cents)/100000
  end
end
