module JamRuby
  class RsvpSlot < ActiveRecord::Base

    belongs_to :instrument, :class_name => "JamRuby::Instrument"
    belongs_to :music_session, :class_name => "JamRuby::MusicSession"
    has_many :rsvp_requests_rsvp_slots, :class_name => "JamRuby::RsvpRequestRsvpSlot", :foreign_key => "rsvp_slot_id"
    has_many :rsvp_requests, :class_name => "JamRuby::RsvpRequest", :through => :rsvp_requests_rsvp_slots

    validates :instrument, presence: true, if: :is_not_unstructured_rsvp?
    validates :is_unstructured_rsvp, :inclusion => {:in => [true, false]}
    validates :proficiency_level, presence: true, if: :is_not_unstructured_rsvp?

    attr_accessor :chosen, :proficiency_desc

    class << self
      @@proficiency_map = ["Any Skill Level", "Beg", "Beg/Int", "Int", "Int/Adv", "Adv"]
    end

    def is_not_unstructured_rsvp?
      !is_unstructured_rsvp?
    end
    # TODO: validates :proficiency_level

    def self.index(music_session)
      RsvpSlot.where("music_session_id = ?", music_session.id)
    end

    def chosen
      chosen_slots = RsvpRequestRsvpSlot.where("chosen = true AND rsvp_slot_id = ?", self.id)
      !chosen_slots.blank?
    end

    def proficiency_desc
      @@proficiency_map[self.proficiency_level]
    end

    # def has_rsvp_from_user(user)
    #   user_slot = RsvpRequest.joins(:rsvp_requests_rsvp_slots)
    #                 .where(:rsvp_request_id => )
    #                 .where(:user_id => user.id)
    # end
  end
end
