module JamRuby

  # Interact with external python tools to create the JKZ
  class GuitarCenter

    def self.init
      user = User.create_user("Anonymous", "Anonymous", "seth+guitarcenter@jamkazam.com", "guitarcenterjam123", "Austin", "TX", "US", nil, nil)
      teacher = Teacher.build_teacher(user, {
                                              biography: 'Biography still in progress...',
                                              teaches_test_drive: true
                                          })

      whitelist = UserWhitelist.new
      whitelist.user = user
      whitelist.save!

      school = School.new
      school.user = user
      school.name = 'Guitar Center'
      school.scheduling_communication = 'school'
      school.special = School::GUITAR_CENTER
      school.correspondence_email = 'seth+guitarcenter@jamkazam.com'
      school.save!

      school.reload
      school.affiliate_partner.special = AffiliatePartner::GUITAR_CENTER
      school.affiliate_partner.partner_name = 'Guitar Center'
      school.affiliate_partner.save!

      retailer = Retailer.new
      retailer.user = school.user
      retailer.name = 'Guitar Center'
      retailer.slug = 'guitar-center'
      retailer.affiliate_partner = school.affiliate_partner
      retailer.special = Retailer::GUITAR_CENTER
      retailer.save!

      return {
          user:user,
          school: school,
          retailer:retailer,
          teacher:teacher
      }
    end

    def self.bootstrap_teacher(email)
      teacher_user = FactoryGirl.create(:teacher_user, email: email)
      teacher_user.teacher.ready_for_session_at = Time.now
      teacher_user.teacher.school = School.guitar_center
      teacher_user.teacher.save!
    end

    def self.bootstrap_user(email, add_lessons)
      user = User.create_user("Anonymous", "Anonymous", email, "guitarcenterjam123", "Austin", "TX", "US", nil, nil)

      if add_lessons
        most_recent_manual_card = PosaCard.where(origin: 'manual').where("code ilike 'JK%'").order('code desc').first
        if most_recent_manual_card
          number_code = most_recent_manual_card.code[2..-1]
          number_code = number_code.to_i
        else
          number_code = 1
        end

        card = PosaCard.new
        card.code = "JK#{"%06d" % number_code}"
        card.card_type = 'jam_class_4'
        card.origin = 'manual'
        card.save!

        gc_retailer = Retailer.guitar_center
        card.activate(gc_retailer)

        card.reload

        card.claim(user)
      else

        user.school = School.guitar_center
        user.is_a_student = true
        user.save!
      end

      user.reload
      user
    end

    # If this is a lesson posa card, then put that user into the guitar center school
    def self.post_posa_claim(posa)
      if posa.is_lesson_posa_card?
        posa.user.is_a_student = true
        # Associate user with guitar center school
        if posa.retailer && posa.retailer.is_guitar_center?
          if posa.user.school_id.nil?
            posa.user.school_id = School.guitar_center.id
          end
          posa.user.save
        end
      end
    end
  end
end
