require 'spec_helper'
require "jam_ruby/recurly_client"
describe RecurlyClient do
  let(:jamtrack) { FactoryGirl.create(:jam_track, plan_code: 'jamtrack-acdc-backinblack') }

  before :all do
    @client = RecurlyClient.new    
    @jamtrack = FactoryGirl.create(:jam_track, plan_code: 'jamtrack-acdc-backinblack')
  end

  before(:each) do
    pending "We have to re-create the Jamkazam test environment in Recurly because it got deleted"

    @user = FactoryGirl.create(:user)
    @billing_info = {}
    @billing_info[:first_name]          = @user.first_name
    @billing_info[:last_name]           = @user.last_name
    @billing_info[:address1]            = 'Test Address 1'
    @billing_info[:address2]            = 'Test Address 2'
    @billing_info[:city]                = @user.city
    @billing_info[:state]               = @user.state
    @billing_info[:country]             = @user.country
    @billing_info[:zip]                 = '12345'
    @billing_info[:number]              = '4111-1111-1111-1111'
    @billing_info[:month]               = '08'
    @billing_info[:year]                = '2017'
    @billing_info[:verification_value]  = '111'    
  end

  after(:each) do
    if @user && @user.recurly_code
      account = Recurly::Account.find(@user.recurly_code)
      if account.present?
        account.destroy    
      end
    end
  end

  it "can create account" do
    account = @client.create_account(@user, @billing_info)    
    account.should_not be_nil
    @user.recurly_code.should eq(account.account_code)
  end

  it "can create account with errors" do    
    @billing_info[:verification_value]  = '1111'    
    expect {@client.create_account(@user, @billing_info)}.to raise_error(RecurlyClientError)    
  end

  describe "with account" do
    before :each do
      @account = @client.find_or_create_account(@user, @billing_info)      
    end

    it "can find account" do          
      @user.recurly_code.should_not be_nil
      found = @client.get_account(@user)
      found.should_not be_nil
      found.first_name.should eq(@user.first_name)
      found.last_name.should eq(@user.last_name)
      found.email.should eq(@user.email)
    end

    it "can update account" do
      @user.first_name = "Foobar"
      @client.update_account(@user, @billing_info)
      found = @client.get_account(@user)
      found.first_name.should eq(@user.first_name)
    end

    it "can update billing" do
      @billing_info['address1'] = "2112 Foobar Lane"
      @client.update_billing_info(@user, @billing_info)
      found = @client.get_account(@user)
      found.billing_info.address1.should eq(@billing_info['address1'])
    end    

    it "purchases jamtrack" do
    end
  end

  it "can remove account" do
    @client.find_or_create_account(@user, @billing_info)      
    @client.get_account(@user).should_not be_nil
    @client.delete_account(@user)
    found=@client.get_account(@user)
    found.should_not be_nil
    found.state.should eq('closed')    
  end

=begin
  it "can refund subscription" do
    sale = Sale.create(@user)
    shopping_cart = ShoppingCart.create @user, @jamtrack, 1
    @client.find_or_create_account(@user, @billing_info)
    
    # Place order:
    expect{@client.place_order(@user, @jamtrack, shopping_cart, sale)}.not_to raise_error()
    active_subs=@client.get_account(@user).subscriptions.find_all{|t|t.state=='active'}
    @jamtrack.reload
    @jamtrack.jam_track_rights.should have(1).items

    # Refund:
    expect{@client.refund_user_subscription(@user, @jamtrack)}.not_to raise_error()    
    active_subs=@client.get_account(@user).subscriptions.find_all{|t|t.state=='active'}
    active_subs.should have(0).items

    @jamtrack.reload
    @jamtrack.jam_track_rights.should have(0).items
  end
=end

end

