require 'spec_helper'

describe RsvpSlot do

  before(:each) do
    SessionInfoComment.delete_all
    Notification.delete_all
    RsvpRequestRsvpSlot.delete_all
    RsvpRequest.delete_all
    RsvpSlot.delete_all
    Invitation.delete_all
    MusicSession.delete_all
    User.delete_all

    @session_invitee = FactoryGirl.create(:user)

    @session_creator = FactoryGirl.create(:user)

    # session invitations require sender and receiver to be friends
    FactoryGirl.create(:friendship, :user => @session_invitee, :friend => @session_creator)
    FactoryGirl.create(:friendship, :user => @session_creator, :friend => @session_invitee)

    @music_session = FactoryGirl.build(:music_session, :creator => @session_creator)
    @music_session.save

    @slot1 = FactoryGirl.create(:rsvp_slot, :music_session => @music_session, :instrument => JamRuby::Instrument.find('electric guitar'))
    
    @slot2 = FactoryGirl.create(:rsvp_slot, :music_session => @music_session, :instrument => JamRuby::Instrument.find('drums'))
    
    @invitation = FactoryGirl.create(:invitation, :sender => @session_creator, :receiver => @session_invitee, :music_session => @music_session)
  end

  describe "index" do
    it "should allow retrieval of all slots for a session" do
      slots = RsvpSlot.index(@music_session)
      slots.count.should == 2
      slots.where(:instrument_id => 'electric guitar').count.should == 1
      slots.where(:instrument_id => 'drums').count.should == 1
    end

    it "should mark slot as chosen after accepting RSVP request" do
      rsvp = RsvpRequest.create({:session_id => @music_session.id, :rsvp_slots => [@slot1.id, @slot2.id], :message => "Let's Jam!"}, @session_invitee)

      # approve with organizer
      rs1 = RsvpRequestRsvpSlot.find_by_rsvp_slot_id(@slot1.id)
      rs2 = RsvpRequestRsvpSlot.find_by_rsvp_slot_id(@slot2.id)
      expect {RsvpRequest.update({:id => rsvp.id, :session_id => @music_session.id, :rsvp_responses => [{:request_slot_id => rs1.id, :accept => true}, {:request_slot_id => rs2.id, :accept => false}]}, @session_creator)}.to_not raise_error

      slots = RsvpSlot.index(@music_session)
      slots.count.should == 2
      slots.where(:instrument_id => 'electric guitar').first.chosen.should == true
      slots.where(:instrument_id => 'drums').first.chosen.should == false
    end

    it "should mark slot as not chosen after canceling RSVP request" do
      rsvp = RsvpRequest.create({:session_id => @music_session.id, :rsvp_slots => [@slot1.id, @slot2.id], :message => "Let's Jam!"}, @session_invitee)

      # approve with organizer
      rs1 = RsvpRequestRsvpSlot.find_by_rsvp_slot_id(@slot1.id)
      rs2 = RsvpRequestRsvpSlot.find_by_rsvp_slot_id(@slot2.id)
      expect {RsvpRequest.update({:id => rsvp.id, :session_id => @music_session.id, :rsvp_responses => [{:request_slot_id => rs1.id, :accept => true}, {:request_slot_id => rs2.id, :accept => false}]}, @session_creator)}.to_not raise_error

      expect {RsvpRequest.cancel({:id => rsvp.id, :session_id => @music_session.id, :cancelled => "yes", :message => "Sorry, you're booted for this session"}, @session_creator)}.to_not raise_error

      # after cancelling RSVP, slots should be marked as chosen again
      slots = RsvpSlot.index(@music_session)
      slots.count.should == 2
      slots.where(:instrument_id => 'electric guitar').first.chosen.should == false
      slots.where(:instrument_id => 'drums').first.chosen.should == false
    end
  end

  describe "validators" do
    it "require instrument if not unstructured" do
      rsvp_slot = FactoryGirl.build(:rsvp_slot, instrument: nil)
      rsvp_slot.valid?.should be_false
      rsvp_slot.errors[:instrument].should == ["can't be blank"]
    end

    it "not require instrument if unstructured" do
      rsvp_slot = FactoryGirl.build(:rsvp_slot, is_unstructured_rsvp: true, instrument: nil)
      rsvp_slot.valid?.should be_true
    end
  end
end