require 'spec_helper'

describe ApiAffiliateController, type: :controller do
  render_views

  let(:partner1) {FactoryGirl.create(:affiliate_partner)}
  let(:user_partner1) { partner1.partner_user }
  let(:partner2) {FactoryGirl.create(:affiliate_partner)}
  let(:user_partner2) { partner2.partner_user }

  before(:each) do
    controller.current_user = user_partner1
  end

  describe "traffic_index" do
    it "empty" do
      get :traffic_index
      response.should be_success
      JSON.parse(response.body)['traffics'].should eq([])
    end

    it "single item" do
      traffic_total = FactoryGirl.create(:affiliate_traffic_total, affiliate_partner: partner1, visits: 5, signups: 0, day:Date.today)

      get :traffic_index
      response.should be_success
      JSON.parse(response.body)['traffics'].should eq([{"day" =>  Date.today.to_s, "visits" => 5, "signups" => 0, "affiliate_partner_id" => partner1.id}])
    end
  end

  describe "monthly_index" do
    it "empty" do
      get :monthly_index
      response.should be_success
      JSON.parse(response.body)['monthlies'].should eq([])
    end

    it "single item" do
      monthly = FactoryGirl.create(:affiliate_monthly_payment, affiliate_partner: partner1, closed: true, due_amount_in_cents: 20, month: 1, year: 2015)

      get :monthly_index
      response.should be_success
      JSON.parse(response.body)['monthlies'].should eq([{"closed" => true, "month" =>  1, "year" => 2015, "due_amount_in_cents" => 20, "affiliate_partner_id" => partner1.id}])
    end
  end


  describe "quarterly_index" do
    it "empty" do
      get :quarterly_index
      response.should be_success
      JSON.parse(response.body)['quarterlies'].should eq([])
    end

    it "single item" do
      quarterly = FactoryGirl.create(:affiliate_quarterly_payment, affiliate_partner: partner1, paid: true, closed: true, due_amount_in_cents: 20, quarter: 1, year: 2015)

      get :quarterly_index
      response.should be_success
      JSON.parse(response.body)['quarterlies'].should eq([{"closed" => true, "paid" => true, "quarter" =>  1, "year" => 2015, "due_amount_in_cents" => 20, "affiliate_partner_id" => partner1.id}])
    end


    it "not paid is excluded" do
      quarterly = FactoryGirl.create(:affiliate_quarterly_payment, affiliate_partner: partner1, paid: false, closed: true, due_amount_in_cents: 20, quarter: 1, year: 2015)

      get :quarterly_index
      response.should be_success
      JSON.parse(response.body)['quarterlies'].should eq([])
    end


    it "not closed is excluded" do
      quarterly = FactoryGirl.create(:affiliate_quarterly_payment, affiliate_partner: partner1, paid: true, closed: false, due_amount_in_cents: 20, quarter: 1, year: 2015)

      get :quarterly_index
      response.should be_success
      JSON.parse(response.body)['quarterlies'].should eq([])
    end
  end

  describe "payment_index" do
    it "empty" do
      get :payment_index
      response.should be_success
      JSON.parse(response.body)['payments'].should eq([])
    end

    it "presents single JamTrack item" do
      FactoryGirl.create(:affiliate_monthly_payment, affiliate_partner: partner1, closed: true, due_amount_in_cents: 20, month: 1, year: 2015, jamtracks_sold: 1)

      get :payment_index
      response.should be_success
      JSON.parse(response.body)['payments'].should eq([{"closed" => true, "paid" => nil, "payment_type" => "monthly", "quarter" => nil, "month" =>  1, "year" => 2015, "due_amount_in_cents" => 20, "affiliate_partner_id" => partner1.id, "jamtracks_sold" => 1, "subscriptions" => [] }])
    end

    it "presents subscriptions" do
       #Silver plan subscription on January
       FactoryGirl.create(:affiliate_distribution, 
        product_type: 'Subscription',
        product_code: 'jamsubsilver',
        affiliate_referral: partner1,
        affiliate_referral_fee_in_cents: 15,
        created_at: Date.new(2015, 1, 1)
      )
      AffiliatePartner.tally_up(Date.new(2015, 1, 1))

      get :payment_index
      response.should be_success
      expect(JSON.parse(response.body)['payments']).to have(6).things
      expect(JSON.parse(response.body)['payments']).to include({"closed" => false, "paid" => nil, "payment_type" => "monthly", "quarter" => nil, "month" =>  1, "year" => 2015, "due_amount_in_cents" => 15, "affiliate_partner_id" => partner1.id, "jamtracks_sold" => 0, "subscriptions" => [{ "plan" => "jamsubsilver", "count" => 1 }] })
    end
  end

end
