require 'spec_helper'

describe PosaCard do

  let(:user) {FactoryGirl.create(:user)}
  let(:card) {FactoryGirl.create(:posa_card)}
  let(:card2) {FactoryGirl.create(:posa_card)}
  let(:card_lessons) {FactoryGirl.create(:posa_card_lesson_4)}
  let(:card_lessons_2) {FactoryGirl.create(:posa_card_lesson_2)}
  let(:retailer) {FactoryGirl.create(:retailer)}
  it "created by factory" do
    card.touch
  end

  describe "activated" do
    it "succeeds" do
      card.activate(retailer)

      card.errors.any?.should be false
      card.activated_at.should_not be_nil
      card.retailer.should eql retailer
    end

    it "cant be re-activated" do

      card.activate(retailer)

      Timecop.travel(Time.now + 1)

      card.activate(retailer)

      card.errors.any?.should be true
      card.errors[:activated_at].should eql ['already activated. Please give card to customer. Thank you!']
    end

    it "must have retailer" do

      card.activate(nil)

      card.errors.any?.should be true
      card.errors[:retailer].should eql ['must be specified']
    end
  end

  describe "claim" do
    it "succeeds simple" do
      PosaCard.activate(card, retailer)
      card.reload
      card.claim(user)
      user.reload
      user.jamclass_credits.should be 0

      card.errors.any?.should be false
      card.claimed_at.should_not be_nil
      card.user.should eql user
    end

    it "succeeds with jamclass type" do
      PosaCard.activate(card_lessons, retailer)
      card_lessons.reload
      card_lessons.claim(user)
      user.jamclass_credits.should be 4

      card_lessons.errors.any?.should be false
      card_lessons.claimed_at.should_not be_nil
      card_lessons.user.should eql user
      card_lessons.reload
      card_lessons.lesson_package_purchase.should_not be_nil
      card_lessons.lesson_package_purchase.lesson_package_type.should eql card_lessons.lesson_package_type
      card_lessons.lesson_package_purchase.posa_card.should eql card_lessons
    end

    it "succeeds with jamclass type 2-count" do
      PosaCard.activate(card_lessons_2, retailer)
      card_lessons_2.reload
      card_lessons_2.claim(user)
      user.reload
      user.jamclass_credits.should be 2

      card_lessons_2.errors.any?.should be false
      card_lessons_2.claimed_at.should_not be_nil
      card_lessons_2.user.should eql user
      card_lessons_2.reload
      card_lessons_2.lesson_package_purchase.should_not be_nil
      card_lessons_2.lesson_package_purchase.lesson_package_type.should eql card_lessons_2.lesson_package_type
      card_lessons_2.lesson_package_purchase.posa_card.should eql card_lessons_2
    end

    it "associates student automatically for GC" do
      gc = GuitarCenter.init
      gc_owner = gc[:user]
      gc_school = gc[:school]
      gc_retailer = gc[:retailer]

      PosaCard.activate(card_lessons, gc_retailer)
      card_lessons.reload
      card_lessons.claim(user)

      user.reload
      user.is_guitar_center_student?.should be_true
      card_lessons.errors.any?.should be false
      card_lessons.claimed_at.should_not be_nil
      card_lessons.user.should eql user
      card_lessons.reload
      card_lessons.lesson_package_purchase.should_not be_nil
      card_lessons.lesson_package_purchase.lesson_package_type.should eql card_lessons.lesson_package_type
      card_lessons.lesson_package_purchase.posa_card.should eql card_lessons
    end


    it "must be already activated" do

      card.claim(user)

      card.errors.any?.should be true
      card.errors[:activated_at].should eql ['must already be set']
    end

    it "cant be re-claimed" do

      PosaCard.activate(card, retailer)
      card.reload
      card.claim(user)

      Timecop.travel(Time.now + 1)

      card.claim(user)

      card.errors.any?.should be true
      card.errors[:claimed_at].should eql ['already claimed']
    end

    it "must have user" do
      PosaCard.activate(card, retailer)
      card.reload
      card.claim(nil)

      card.errors.any?.should be true
      card.errors[:user].should eql ['must be specified']
    end

    it "can't be within one year" do
      PosaCard.activate(card_lessons, retailer)
      card_lessons.reload
      card_lessons.claim(user)

      PosaCard.activate(card_lessons_2, retailer)
      card_lessons_2.reload
      card_lessons_2.claim(user)

      card_lessons_2.errors.any?.should be true
      card_lessons_2.errors[:claimed_at].should eql ['was within 1 year']
    end
  end
end