require 'spec_helper'

describe CueCloudApi do

  let(:test_api_key) {'42cef2c79a984e34'}
  let(:test_api_pass) {'2152b0f3cc1649fb'}

  let(:test_cc_last_four) { '1111' }
  let(:pay_amount) { '%.2f' % (10.24 + rand).to_f }
  let(:num_opportunities) { 4 }
  let(:cuecloud_fee) {0.10} #10%

  subject(:cuecloud) { CueCloudApi.new(test_api_key, test_api_pass) }

  describe '.validate_user' do
    it 'validates the user account and status code 200 is returned' do
      expect(cuecloud.validate_user["StatusCode"]).to eq(200)
    end
  end

  describe '.get_keywords' do
    it 'get the keywords' do
      expect(cuecloud.get_keywords['Data']['Keywords']).to include('facebook')
    end
  end

  describe '.make_deposit' do
    it 'add funds to user account' do
      previous_balance = cuecloud.get_balance['Data']['Balance']
      cuecloud.make_deposit(pay_amount, test_cc_last_four)
      new_balance = cuecloud.get_balance['Data']['Balance']
      expect((new_balance - previous_balance).abs).to be_within(0.1).of(pay_amount.to_f)
    end
  end

  describe '.withdraw_funds' do
    it 'subtract funds from user account' do
      previous_balance = cuecloud.get_balance['Data']['Balance']
      cuecloud.withdraw_funds(pay_amount)
      new_balance = cuecloud.get_balance['Data']['Balance']
      expect((new_balance - previous_balance).abs).to be_within(0.1).of(pay_amount.to_f)
    end
  end

  describe '.create_cue' do
    it 'creates a cue' do
      previous_num_cues = cuecloud.get_cues['Data']['NumTotalResults']
      previous_balance = cuecloud.get_balance['Data']['Balance']
      new_cue = cuecloud.create_cue("New Cue", pay_amount, num_opportunities: num_opportunities)
      new_num_cues = cuecloud.get_cues['Data']['NumTotalResults']
      new_balance = cuecloud.get_balance['Data']['Balance']
      expect(new_num_cues).to be > previous_num_cues
      balance_difference = (previous_balance - new_balance).abs
      difference_should_be =(pay_amount.to_f * num_opportunities * (1 + cuecloud_fee)).to_f
      expect(balance_difference).to be_within(difference_should_be).of(0.1)
    end
  end

  describe '.submit_cue_completion' do
     it 'submits a cue completion' do
       last_cue_id = cuecloud.get_cues['Data']['Cues'][0]['ID']
       previous_num_completions = cuecloud.get_cue_completions(last_cue_id)['Data']['NumTotalResults']
       assignment_id = cuecloud.assign_cue(last_cue_id)['Data']['AssignmentID']
       cuecloud.submit_cue_completion( assignment_id, answer_text: 'My Answer')['Data']['CueCompletionID']
       new_num_completions = cuecloud.get_cue_completions(last_cue_id)['Data']['NumTotalResults']
       expect(new_num_completions).to be > previous_num_completions

     end
  end

  describe '.grant_bonus' do
    it 'grant a bonus' do
      previous_num_payments = cuecloud.get_payments['Data']['NumTotalResults']
      last_cue_id = cuecloud.get_cues(has_pending_cue_completions: true)['Data']['Cues'][0]['ID']
      last_cue_completion_id = cuecloud.get_cue_completions(last_cue_id)['Data']['CueCompletions'][0]['ID']
      cuecloud.grant_bonus(cue_completion_id=last_cue_completion_id, amount=pay_amount, reason='Nice work')
      new_num_payments = cuecloud.get_payments['Data']['NumTotalResults']
      expect(new_num_payments).to be > previous_num_payments
    end
  end

  describe '.get_balance' do
    it "returns the user's current balance" do
      expect(cuecloud.get_balance["Data"]["Balance"]).to be_a(Numeric)
    end
  end

  describe '.get_cues' do
    it "returns all thec clues the user has created" do
      expect(cuecloud.get_cues["Data"]["NumTotalResults"]).to be_a(Numeric)
    end
  end

end
