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(:user_partner) { FactoryGirl.create(:user, affiliate_referral: partner) }
  let(:jam_track) {FactoryGirl.create(:jam_track)}
  let(:sale) {Sale.create_jam_track_sale(user_partner)}

  before(:each) do
    JamTrackRight.delete_all
    JamTrack.delete_all
    AffiliateQuarterlyPayment.delete_all
    AffiliateMonthlyPayment.delete_all
    AffiliateTrafficTotal.delete_all
    AffiliateDistribution.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
     
      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').click
      #find('#account-affiliate-partner tr td.target')
      find('table.links-table')

      # 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').click
      find('table.traffic-table')

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

      find('a#affiliate-partner-agreement-link').click
      find('h2', text: 'JamKazam Affiliate Agreement')
      find('span.c0', text: 'Updated: February 9, 2021.')

      #save_screenshot("account_affiliate_agreement.png")
    end

    it "shows signups 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-01-01')
      FactoryGirl.create(:affiliate_traffic_total, affiliate_partner: partner, day: day1, signups: 1, visits: 5)
      day2 = Date.parse('2015-01-15')
      FactoryGirl.create(:affiliate_traffic_total, affiliate_partner: partner, day: day2, signups: 5, visits: 10)
      day3 = Date.parse('2015-01-31')
      FactoryGirl.create(:affiliate_traffic_total, affiliate_partner: partner, day: day3, signups: 10, visits: 20)

      day4 = Date.parse('2015-02-01')
      FactoryGirl.create(:affiliate_traffic_total, affiliate_partner: partner, day: day4, signups: 1, visits: 2)
      
      day5 = Date.parse('2015-03-15')
      FactoryGirl.create(:affiliate_traffic_total, affiliate_partner: partner, day: day5, signups: 10, visits: 20)

      day6 = Date.parse('2015-04-01')
      FactoryGirl.create(:affiliate_traffic_total, affiliate_partner: partner, day: day6, signups: 20, visits: 50)
      
      day7 = Date.parse('2015-04-05')
      FactoryGirl.create(:affiliate_traffic_total, affiliate_partner: partner, day: day7, signups: 5, visits: 10)

      find('a#affiliate-partner-signups-link').click

      months  = page.all('table.traffic-table tr td.month')
      signups = page.all('table.traffic-table tr td.signups')
      visits  = page.all('table.traffic-table tr td.visits')

      months[0].should have_content("April")
      months[1].should have_content("March")
      months[2].should have_content("February")
      months[3].should have_content("January")

      signups[0].should have_content("25")
      signups[1].should have_content("10")
      signups[2].should have_content("1")
      signups[3].should have_content("16")

      visits[0].should have_content("60")
      visits[1].should have_content("20")
      visits[2].should have_content("2")
      visits[3].should have_content("35")

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

      day8 = Date.parse('2015-04-07')
      FactoryGirl.create(:affiliate_traffic_total, affiliate_partner: partner, day: day8, signups: 5, visits: 10)
      
      find('a#affiliate-partner-signups-link').click

      months  = page.all('table.traffic-table tr td.month')
      signups = page.all('table.traffic-table tr td.signups')
      visits  = page.all('table.traffic-table tr td.visits')

      months[0].should have_content("April")
      signups[0].should have_content("30")
      visits[0].should have_content("70")

      #save_screenshot("account_affiliate_signup_links.png")

    end

    it "shows earnings" do
      jam_track.touch

      visit "/client#/account/affiliatePartner"
      find('.tab-account', text: 'So please provide this data, and be sure to keep it current!')
      
      # verify earnings data correctly
      FactoryGirl.create(:affiliate_monthly_payment, affiliate_partner:partner, year:2015, month:1, due_amount_in_cents:25, jamtracks_sold: 1, closed:true)

      find('a#affiliate-partner-earnings-link').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.25')

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

      FactoryGirl.create(:affiliate_monthly_payment, affiliate_partner:partner, year:2015, month:2, due_amount_in_cents:50, jamtracks_sold: 2, closed:true)
      FactoryGirl.create(:affiliate_monthly_payment, affiliate_partner:partner, year:2015, month:3, due_amount_in_cents:75, jamtracks_sold: 3, closed:true)
      FactoryGirl.create(:affiliate_monthly_payment, affiliate_partner:partner, year:2015, month:4, due_amount_in_cents:0, jamtracks_sold: 0, closed:true)
      

      find('a#affiliate-partner-earnings-link').click


      months = page.all("table.payment-table tr td.month")
      sales = page.all("table.payment-table tr td.sales")
      earnings = page.all("table.payment-table tr td.earnings")

      months[0].should have_content("April 2015")
      months[1].should have_content("March 2015")
      months[2].should have_content("February 2015")
      months[3].should have_content("January 2015")

      sales[0].should have_content("")
      sales[1].should have_content("JamTracks: 3 units sold")
      sales[2].should have_content("JamTracks: 2 units sold")
      sales[3].should have_content("JamTracks: 1 unit sold")

      earnings[0].should have_content("")
      earnings[1].should have_content("$0.75")
      earnings[2].should have_content("$0.50")
      earnings[3].should have_content("$0.25")

       #save_screenshot("account_affiliate_earnings.png")
    end

    it "shows earnings by Subscription and JamTrack sales" do
      #Silver plan subscription on January
      FactoryGirl.create(:affiliate_distribution, 
        product_type: 'Subscription',
        product_code: 'jamsubsilver',
        affiliate_referral: partner,
        affiliate_referral_fee_in_cents: 15,
        created_at: Date.new(2015, 1, 1)
      )

      #JamTrack sale on January
      jam_track.reload
      shopping_cart = ShoppingCart.create user_partner, jam_track, 1, false
      real_sale = SaleLineItem.create_from_shopping_cart(sale, shopping_cart, nil, nil, nil)
      real_sale.affiliate_referral_fee_in_cents.should eq(25)
      real_sale.created_at = Date.new(2015, 1, 1)
      real_sale.save!
      real_sale.affiliate_distributions.first.created_at = real_sale.created_at
      real_sale.affiliate_distributions.first.save!


      #Gold plan subscription on January
      FactoryGirl.create(:affiliate_distribution, 
        product_type: 'Subscription',
        product_code: 'jamsubgold',
        affiliate_referral: partner,
        affiliate_referral_fee_in_cents: 30,
        created_at: Date.new(2015, 1, 1)
      )
      
      #Platinum plan subscription in February
      FactoryGirl.create(:affiliate_distribution, 
        product_type: 'Subscription',
        product_code: 'jamsubplatinum',
        affiliate_referral: partner,
        affiliate_referral_fee_in_cents: 60,
        created_at: Date.new(2015, 2, 1)
      )

      AffiliatePartner.tally_up(Date.new(2015, 2, 1))

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

      find('a#affiliate-partner-earnings-link').click
      
      # within('table.payment-table') do
      #   find('tr td.month', text: "January 2015")
      #   find('tr td.sales', text: 'JamTracks: 1 unit sold')
      #   find('tr td.subscriptions', text: 'Gold subscriptions - 1')
      #   find('tr td.subscriptions', text: 'Silver subscriptions - 1')
      #   find('tr td.earnings', text: '$1.70')
      # end

      months = page.all("table.payment-table tbody tr td.month")
      months[0].should have_content("March 2015")
      months[1].should have_content("February 2015")
      months[2].should have_content("January 2015")

      sales = page.all("table.payment-table tbody tr td.sales")
      sales[0].should have_content("JamTracks: 0 units sold")
      sales[1].should have_content("JamTracks: 0 units sold")
      sales[2].should have_content("JamTracks: 1 unit sold")

      subscriptions = page.all("table.payment-table tbody tr td.subscriptions")
      subscriptions[0].should have_content("")
      subscriptions[1].should have_content("")
      subscriptions[2].should have_content("Gold subscriptions - 1")
      subscriptions[2].should have_content("Silver subscriptions - 1")

      earnings = page.all("table.payment-table tbody tr td.earnings")
      earnings[0].should have_content("")
      earnings[1].should have_content("$0.60")
      earnings[2].should have_content("$0.70")

      #save_screenshot("account_affiliate_earnings_with_subscriptions.png")

    end


    # it "shows data" do
    #   sign_in_poltergeist partner.partner_user

    #   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('a#affiliate-partner-signups-link').click
    #   find('table.traffic-table tr td.month', text: "April 2015")
    #   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('a#affiliate-partner-earnings-link').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('a#affiliate-partner-signups-link').click
    #   find('table.traffic-table tr td.month', text: "April 2015")
    #   find('table.traffic-table tr td.signups', text: '4')
    #   find('table.traffic-table tr td.visits', text: '6')

    #   # 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('a#affiliate-partner-earnings-link').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('a#affiliate-partner-signups-link').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('a#affiliate-partner-earnings-link').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('a#affiliate-partner-signups-link').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('a#affiliate-partner-earnings-link').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
