require 'spec_helper'

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

  subject { page }

  let(:user) { FactoryGirl.create(:user, traditional_band: true,paid_sessions: true, paid_sessions_hourly_rate: 1,  paid_sessions_daily_rate:1 ) }
  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
    sign_in_poltergeist user
    visit "/client#/account"

    find('div.account-mid.identity')
  end

  describe "payment history" do
    it "show 1 sale" do

      sale = Sale.create_jam_track_sale(user)
      shopping_cart = ShoppingCart.create(user, jam_track)
      sale_line_item = SaleLineItem.create_from_shopping_cart(sale, shopping_cart, nil, 'some_adjustment_uuid', nil)

      visit "/client#/account"

      find('.account-mid.payments', text: 'You have made 1 purchase.')

      find("#account-payment-history-link").trigger(:click)
      find('.account-header', text: 'payment history:')
      find('table tr td', text: '$0.00') # 1st purchase is free

      find('.profile-tile.student a', text: 'payment method').trigger(:click)


      fill_in 'card-number', with: '4111111111111111'
      fill_in 'expiration', with: '11/2016'
      fill_in 'cvv', with: '111'
      fill_in 'zip', with: '78759'

      find('.purchase-btn').trigger(:click)

      find('a.update-btn', text: "I'D LIKE TO UPDATE MY PAYMENT INFO").trigger(:click)

      user.reload
      user.stripe_customer_id.should_not be_nil
      user.stripe_token.should_not be_nil
      original_token = user.stripe_token

      fill_in 'card-number', with: '4111111111111111'
      fill_in 'expiration', with: '11/2016'
      fill_in 'cvv', with: '111'
      fill_in 'zip', with: '78759'

      find('.purchase-btn').trigger(:click)

      find('a.update-btn', text: "I'D LIKE TO UPDATE MY PAYMENT INFO").trigger(:click)

      user.reload
      original_token.should_not eql user.stripe_token
    end
  end

  it "handles unpaid lessons" do
    teacher = FactoryGirl.create(:teacher_user)
    lesson_session = normal_lesson(user, teacher)
    lesson_session.lesson_payment_charge.user.should eql user
    lesson_session.lesson_payment_charge.billing_attempts = 1
    lesson_session.lesson_payment_charge.save!
    uncollectables = user.uncollectables
    uncollectables.count.should eql 1

    visit "/client#/account"

    find('.account-mid.payments', text: 'You have made no purchases.')
    sleep 2
    find("#account-payment-history-link").trigger(:click)
    find('.account-header', text: 'payment history:')

    find('.uncollectable-msg', text: 'You have unpaid lessons')
    find('.uncollectable-msg a').trigger(:click)


    fill_in 'card-number', with: '4111111111111111'
    fill_in 'expiration', with: '11/2016'
    fill_in 'cvv', with: '111'
    fill_in 'zip', with: '78759'

    find('.purchase-btn').trigger(:click)

    find('#banner .dialog-inner', text: 'Your credit card info has been updated')

    # dismiss banner
    find('a.button-orange', text:'CLOSE').trigger(:click)

    user.reload

    user.stripe_customer_id.should_not be_nil
    user.stripe_token.should_not be_nil
  end
end
