require 'spec_helper'

describe "Account Affiliate", :js => true, :type => :feature, :capybara_feature => true do

  subject { page }

  let(:user) {FactoryGirl.create(:user)}
  let(:partner) { FactoryGirl.create(:affiliate_partner) }
  let(:jam_track) {FactoryGirl.create(:jam_track)}

  before(:each) do
    JamTrackRight.delete_all
    JamTrack.delete_all
    AffiliateQuarterlyPayment.delete_all
    AffiliateMonthlyPayment.delete_all
    AffiliateTrafficTotal.delete_all
    UserMailer.deliveries.clear
    emulate_client
  end

  describe "account overview" do
    it "shows correct values for partner" do
      partner.referral_user_count = 3
      partner.cumulative_earnings_in_cents = 10000
      partner.save!

      sign_in_poltergeist partner.partner_user
      visit "/client#/account"
      find('.account-mid.affiliate .user-referrals', text: 'You have referred 3 users to date.')
      find('.account-mid.affiliate .affiliate-earnings', text: 'You have earned $100.00 to date.')
    end

    it "shows correct values for unaffiliated user" do
      sign_in_poltergeist user
      visit "/client#/account"
      find('.account-mid.affiliate .not-affiliated', text: 'You are not currently a JamKazam affiliate.')
      find('.account-mid.affiliate .learn-affiliate')
    end
  end

  describe "account affiliate page" do
    before(:each) do
      sign_in_poltergeist partner.partner_user
    end

    it "works on no data" do
      jam_track.touch

      visit "/client#/account/affiliatePartner"
      find('.tab-account', text: 'So please provide this data, and be sure to keep it current!')

      # take a look at the links tab
      find('a#affiliate-partner-links-link').trigger(:click)

      find('#account-affiliate-partner tr td.target')

      # can't find this on the page for some reason:
      #jk_select('Custom Link', '#account-affiliate-partner select.link_type')
      #find('.link-type-prompt[data-type="custom_links"]')

      find('a#affiliate-partner-signups-link').trigger(:click)
      find('table.traffic-table')

      find('a#affiliate-partner-earnings-link').trigger(:click)
      find('table.payment-table')

      find('a#affiliate-partner-agreement-link').trigger(:click)
      find('h2', text: 'JamKazam Affiliate Agreement')
      find('span.c0', text: 'Updated: April 30, 2015')
    end

    it "shows data" do
      visit "/client#/account/affiliatePartner"
      find('.tab-account', text: 'So please provide this data, and be sure to keep it current!')

      # verify traffic data shows correctly
      day1 = Date.parse('2015-04-05')
      FactoryGirl.create(:affiliate_traffic_total, affiliate_partner: partner, day: day1, signups: 1, visits:2)

      find('a#affiliate-partner-signups-link').trigger(:click)
      find('table.traffic-table tr td.day', text: "April 5")
      find('table.traffic-table tr td.signups', text: '1')
      find('table.traffic-table tr td.visits', text: '2')


      find('a#affiliate-partner-earnings-link').trigger(:click)
      find('table.payment-table')

      day2 = Date.parse('2015-04-07')
      FactoryGirl.create(:affiliate_traffic_total, affiliate_partner: partner, day: day2, signups: 3, visits:4)
      find('a#affiliate-partner-signups-link').trigger(:click)
      find('table.traffic-table tr td.day', text: "April 7")
      find('table.traffic-table tr td.signups', text: '3')
      find('table.traffic-table tr td.visits', text: '4')

      # verify earnings data correctly
      FactoryGirl.create(:affiliate_monthly_payment, affiliate_partner:partner, year:2015, month:1, due_amount_in_cents:20, jamtracks_sold: 1, closed:true)

      find('a#affiliate-partner-earnings-link').trigger(:click)
      find('table.payment-table tr td.month', text: "January 2015")
      find('table.payment-table tr td.sales', text: 'JamTracks: 1 unit sold')
      find('table.payment-table tr td.earnings', text: '$0.20')


      find('a#affiliate-partner-signups-link').trigger(:click)
      find('table.traffic-table')

      FactoryGirl.create(:affiliate_monthly_payment, affiliate_partner:partner, year:2015, month:2, due_amount_in_cents:40, jamtracks_sold: 2, closed:true)
      FactoryGirl.create(:affiliate_monthly_payment, affiliate_partner:partner, year:2015, month:3, due_amount_in_cents:60, jamtracks_sold: 3, closed:true)
      quarter1 = FactoryGirl.create(:affiliate_quarterly_payment, affiliate_partner:partner, year:2015, quarter:0, due_amount_in_cents:120, jamtracks_sold: 6, closed:true, paid:false)
      FactoryGirl.create(:affiliate_monthly_payment, affiliate_partner:partner, year:2015, month:4, due_amount_in_cents:2000, jamtracks_sold: 100, closed:true)

      find('a#affiliate-partner-earnings-link').trigger(:click)
      find('table.payment-table tr td.month', text: "January 2015")
      find('table.payment-table tr td.sales', text: 'JamTracks: 1 unit sold')
      find('table.payment-table tr td.earnings', text: '$0.20')
      find('table.payment-table tr td.month', text: "February 2015")
      find('table.payment-table tr td.sales', text: 'JamTracks: 2 units sold')
      find('table.payment-table tr td.earnings', text: '$0.40')
      find('table.payment-table tr td.month', text: "March 2015")
      find('table.payment-table tr td.sales', text: 'JamTracks: 3 units sold')
      find('table.payment-table tr td.earnings', text: '$0.60')
      find('table.payment-table tr td.month', text: "1st Quarter 2015")
      find('table.payment-table tr td.earnings', text: 'No earning were paid, as the $10 minimum threshold was not reached.')



      find('a#affiliate-partner-signups-link').trigger(:click)
      find('table.traffic-table')

      quarter1.paid = true
      quarter1.save!
      FactoryGirl.create(:affiliate_quarterly_payment, affiliate_partner:partner, year:2015, quarter:1, due_amount_in_cents:2000, jamtracks_sold: 100, closed:true, paid:true)

      find('a#affiliate-partner-earnings-link').trigger(:click)
      find('table.payment-table tr td.month', text: "January 2015")
      find('table.payment-table tr td.sales', text: 'JamTracks: 1 unit sold')
      find('table.payment-table tr td.earnings', text: '$0.20')
      find('table.payment-table tr td.month', text: "February 2015")
      find('table.payment-table tr td.sales', text: 'JamTracks: 2 units sold')
      find('table.payment-table tr td.earnings', text: '$0.40')
      find('table.payment-table tr td.month', text: "March 2015")
      find('table.payment-table tr td.sales', text: 'JamTracks: 3 units sold')
      find('table.payment-table tr td.earnings', text: '$0.60')

      find('table.payment-table tr td.month', text: "1st Quarter 2015")
      find('table.payment-table tr td.earnings', text: 'PAID $1.20')
      find('table.payment-table tr td.month', text: "2nd Quarter 2015")
      find('table.payment-table tr td.earnings', text: 'PAID $20.00')
    end
  end
end
