module JamRuby
  # UserMailer must be configured to work
  # Some common configs occur in jam_ruby/init.rb
  # Environment specific configs occur in spec_helper.rb in jam-ruby and jam-web (to put it into test mode),
  # and in config/initializers/email.rb in rails to configure sendmail account settings
  # If UserMailer were to be used in another project, it would need to be configured there, as well.

  # Templates for UserMailer can be found in jam_ruby/app/views/jam_ruby/user_mailer
  class UserMailer < ActionMailer::Base
    include SendGrid
    include MailerHelper

    layout "user_mailer"

    DEFAULT_SENDER = "JamKazam <noreply@jamkazam.com>"

    default :from => DEFAULT_SENDER

    sendgrid_category :use_subject_lines
    #sendgrid_enable   :opentrack, :clicktrack  # this makes our emails creepy, imo (seth)
    sendgrid_unique_args :env => Environment.mode

    def confirm_email(user, signup_confirm_url)
      @user = user
      @signup_confirm_url = "#{APP_CONFIG.external_root_url}/confirm/#{user.signup_token}"
      sendgrid_category "Confirm Email"
      sendgrid_unique_args :type => "confirm_email"

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => user.email, :subject => "Please confirm your JamKazam email") do |format|
        format.text
        format.html
      end
    end

    def welcome_message(user, reset_url = nil)
      @user = user
      @reset_url = reset_url
      sendgrid_category "Welcome"
      sendgrid_unique_args :type => "welcome_message"

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])
      sendgrid_substitute(EmailBatchProgression::VAR_FIRST_NAME, [user.first_name])

      mail(:to => user.email, :subject => "Welcome to JamKazam") do |format|
        format.text
        format.html
      end
    end

    def notify_admin_plan(user)
      if user.admin_override_plan_code.nil?
        puts "INVALID NOTIFY ADMIN SUBSCRIPTION FOR USER #{user.email}"
        return nil
      end

      @user = user
      @subscription_rules = SubscriptionDefinitions.rules(user.subscription_plan_code)
      @plan_end_at = user.admin_override_ends_at.strftime("%B %-d, %Y")

      sendgrid_category "Subscription"
      sendgrid_unique_args :type => "admin_subscription"

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])
      sendgrid_substitute(EmailBatchProgression::VAR_FIRST_NAME, [user.first_name])

      mail(:to => user.email, :cc => APP_CONFIG.email_support_alias, :subject => "Enjoy your free #{@subscription_rules[:name]} plan until #{@plan_end_at}") do |format|
        format.text
        format.html
      end
    end

    def take_paid_lesson(user)
      @user = user
      @lesson = user.taken_lessons.order(:created_at).last
      @teacher = @lesson.teacher

      if user.sent_take_plesson_email_times == 0
        @subject = "Book your next lesson with your instructor now!"
      elsif user.sent_take_plesson_email_times == 1
        @subject = "Book your next lesson to continue your musical journey"
      else
        @subject = "The best way to improve on your instrument is with a great instructor - book today!"
      end
      sendgrid_category "promo_lesson_reminder"
      sendgrid_unique_args :type => "promo_lesson_reminder"

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => user.email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    def take_second_free_lesson(user)
      @user = user
      @lesson = user.taken_lessons.order(:created_at).last
      @teacher = @lesson.teacher

      if user.sent_take_2nd_flesson_email_times == 0
          @subject = "Book your second free lesson now!"
      elsif user.sent_take_2nd_flesson_email_times == 1
          @subject = "Book your second free lesson to continue your musical journey"
      else
        @subject = "Last reminder to book your next free lesson"
      end

      sendgrid_category "promo_lesson_reminder"
      sendgrid_unique_args :type => "promo_lesson_reminder"

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => user.email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    def take_first_free_lesson(user)
      @user = user
      if user.sent_take_flesson_email_times == 0
        @subject = "Book your first free lesson now!"
      elsif user.sent_take_flesson_email_times == 1
        @subject = "Book your first free lesson to start your musical journey"
      else
        @subject = "Last reminder to book your free lessons - $60 value!"
      end

      sendgrid_category "promo_lesson_reminder"
      sendgrid_unique_args :type => "promo_lesson_reminder"

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => user.email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    def onboarding_survey(user)
      @user = user
      @subject = "1-minute JamKazam survey - please help us give good support!"
      sendgrid_category "onboarding_survey"
      sendgrid_unique_args :type => "onboarding_survey"

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => user.email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    def student_education_welcome_message(user)
      @user = user
      @subject = "Welcome to JamKazam and JamClass online lessons!"
      @education = user.school && user.school.education
      sendgrid_category "Welcome"
      sendgrid_unique_args :type => "welcome_message"

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])
      sendgrid_substitute(EmailBatchProgression::VAR_FIRST_NAME, [user.first_name])

      mail(:to => user.email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    def teacher_no_comm_other(lesson_booking)
      user = lesson_booking.teacher
      @student = lesson_booking.student
      @lesson_booking = lesson_booking
      @suppress_user_has_account_footer = true

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])

      sendgrid_unique_args :type => "teacher_no_comm_other"
      mail(:to => user.email, :subject => "JamKazam is attempting to contact "  + @student.name) do |format|
        format.text
        format.html
      end
    end

    def student_no_comm_other(lesson_booking, really_late)
      user = lesson_booking.student
      @teacher = lesson_booking.teacher
      @lesson_booking = lesson_booking
      @really_late = really_late
      @suppress_user_has_account_footer = true
      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])

      sendgrid_unique_args :type => "student_no_comm_other"
      mail(:to => user.email, :subject => "JamKazam is attempting to contact "  + @teacher.name) do |format|
        format.text
        format.html
      end
    end

    def amazon_welcome_message(user)
      @user = user
      @subject = "Your 2 free music lessons"
      sendgrid_category "Welcome"
      sendgrid_unique_args :type => "welcome_message"

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])
      sendgrid_substitute(EmailBatchProgression::VAR_FIRST_NAME, [user.first_name])

      mail(:to => user.email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    def student_welcome_message(user)
      @user = user
      @subject = "Welcome to JamKazam and JamClass online lessons!"
      @education = user.school && user.school.education
      sendgrid_category "Welcome"
      sendgrid_unique_args :type => "welcome_message"

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])
      sendgrid_substitute(EmailBatchProgression::VAR_FIRST_NAME, [user.first_name])

      mail(:to => user.email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    def school_welcome_message(user, reset_url)
      @user = user
      if reset_url
        @subject= "Set a password on your new JamKazam account for your #{user.school.name} music program"
      else
        @subject= "Your JamKazam account has been linked to your #{user.school.name} music program"
      end

      @school = user.school
      @reset_url = reset_url

      sendgrid_category "Welcome"
      sendgrid_unique_args :type => "welcome_message"

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])
      sendgrid_substitute(EmailBatchProgression::VAR_FIRST_NAME, [user.first_name])

      mail(:to => user.email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    def teacher_welcome_message(user)
      @user = user
      @subject= "Welcome to JamKazam and JamClass online lessons!"

      @education = user.teacher && user.teacher.school && user.teacher.school.education

      sendgrid_category "Welcome"
      sendgrid_unique_args :type => "welcome_message"

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])
      sendgrid_substitute(EmailBatchProgression::VAR_FIRST_NAME, [user.first_name])

      mail(:to => user.email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    def school_owner_welcome_message(user)
      @user = user
      @subject= "Welcome to JamKazam and JamClass online lessons!"
      sendgrid_category "Welcome"
      sendgrid_unique_args :type => "welcome_message"

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])
      sendgrid_substitute(EmailBatchProgression::VAR_FIRST_NAME, [user.first_name])

      mail(:to => user.email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    def retailer_owner_welcome_message(user)
      @user = user
      @subject= "Welcome to JamKazam!"
      sendgrid_category "Welcome"
      sendgrid_unique_args :type => "welcome_message"

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])
      sendgrid_substitute(EmailBatchProgression::VAR_FIRST_NAME, [user.first_name])

      mail(:to => user.email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    def education_owner_welcome_message(user)
      @user = user
      @subject= "Welcome to JamKazam and JamClass online lessons!"
      sendgrid_category "Welcome"
      sendgrid_unique_args :type => "welcome_message"

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])
      sendgrid_substitute(EmailBatchProgression::VAR_FIRST_NAME, [user.first_name])

      mail(:to => user.email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    def password_changed(user)
      @user = user

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])

      sendgrid_unique_args :type => "password_changed"
      mail(:to => user.email, :subject => "JamKazam Password Changed") do |format|
        format.text
        format.html
      end
    end

    def password_reset(user, password_reset_url)
      @user = user

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])

      @password_reset_url = password_reset_url
      sendgrid_unique_args :type => "password_reset"
      mail(:to => user.email, :subject => "JamKazam Password Reset") do |format|
        format.text
        format.html
      end
    end

    def updating_email(user)
      @user = user

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])

      sendgrid_unique_args :type => "updating_email"
      mail(:to => user.update_email, :subject => "JamKazam Email Change Confirmation") do |format|
        format.text
        format.html
      end
    end


    def updated_email(user)
      @user = user

      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])

      sendgrid_unique_args :type => "updated_email"
      mail(:to => user.email, :subject => "JamKazam Email Changed") do |format|
        format.text
        format.html
      end
    end

    def new_musicians(user, new_musicians, host='www.jamkazam.com')
      @user, @new_musicians, @host = user, new_musicians, host
      sendgrid_recipients([user.email])
      sendgrid_substitute('@USERID', [user.id])
      sendgrid_unique_args :type => "new_musicians"

      mail(:to => user.email, :subject => EmailBatchNewMusician.subject) do |format|
        format.text
        format.html
      end
    end

    #################################### NOTIFICATION EMAILS ####################################
    def friend_request(user, msg, friend_request_id)
      return if !user.subscribe_email

      @user = user

      email = user.email
      subject = "You have a new friend request on JamKazam"
      unique_args = {:type => "friend_request"}

      @url = Nav.accept_friend_request_dialog(friend_request_id)
      @body = msg
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html
      end
    end

    def friend_request_accepted(user, msg)
      return if !user.subscribe_email

      @user = user

      email = user.email
      subject = "You have a new friend on JamKazam"
      unique_args = {:type => "friend_request_accepted"}

      @body = msg
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html
      end
    end

    def new_user_follower(user, msg)
      return if !user.subscribe_email

      @user = user

      email = user.email
      subject = "You have a new follower on JamKazam"
      unique_args = {:type => "new_user_follower"}

      @body = msg
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html
      end
    end

    def new_band_follower(user, msg)
      return if !user.subscribe_email

      @user = user

      email = user.email
      subject = "Your band has a new follower on JamKazam"
      unique_args = {:type => "new_band_follower"}

      @body = msg
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html
      end
    end

    def session_invitation(user, msg)
      return if !user.subscribe_email

      @user = user


      email = user.email
      subject = "You have been invited to a session on JamKazam"
      unique_args = {:type => "session_invitation"}

      @body = msg
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html
      end
    end

    def musician_session_join(user, msg, session_id)
      return if !user.subscribe_email

      @user = user


      email = user.email
      subject = "Someone you know is in a session on JamKazam"
      unique_args = {:type => "musician_session_join"}
      @body = msg
      @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session_id}"
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html
      end
    end

    def scheduled_session_invitation(user, msg, session)
      return if !user.subscribe_email

      @user = user


      email = user.email
      subject = "Session Invitation"
      unique_args = {:type => "scheduled_session_invitation"}
      @body = msg
      @session_name = session.name
      @session_date = session.pretty_scheduled_start(true)
      @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session.id}/details"
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html
      end
    end

    def scheduled_session_rsvp(user, msg, session)
      return if !user.subscribe_email

      @user = user
      email = user.email
      subject = "Session RSVP"
      unique_args = {:type => "scheduled_session_rsvp"}
      @body = msg
      @session_name = session.name
      @session_date = session.pretty_scheduled_start(true)
      @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session.id}/details"
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html
      end
    end

    def scheduled_session_rsvp_approved(user, msg, session)
      return if !user.subscribe_email

      @user = user


      email = user.email
      subject = "Session RSVP Approved"
      unique_args = {:type => "scheduled_session_rsvp_approved"}
      @body = msg
      @session_name = session.name
      @session_date = session.pretty_scheduled_start(true)
      @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session.id}/details"
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html
      end
    end

    def scheduled_session_rsvp_cancelled(user, msg, session)
      return if !user.subscribe_email

      @user = user


      email = user.email
      subject = "Session RSVP Cancelled"
      unique_args = {:type => "scheduled_session_rsvp_cancelled"}
      @body = msg
      @session_name = session.name
      @session_date = session.pretty_scheduled_start(true)
      @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session.id}/details"
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html
      end
    end

    def scheduled_session_rsvp_cancelled_org(user, msg, session)
      return if !user.subscribe_email

      @user = user


      email = user.email
      subject = "Your Session RSVP Cancelled"
      unique_args = {:type => "scheduled_session_rsvp_cancelled_org"}
      @body = msg
      @session_name = session.name
      @session_date = session.pretty_scheduled_start(true)
      @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session.id}/details"
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html
      end
    end

    def scheduled_session_cancelled(user, msg, session)
      return if !user.subscribe_email

      @user = user

      email = user.email
      subject = "Session Cancelled"
      unique_args = {:type => "scheduled_session_cancelled"}
      @body = msg
      @session_name = session.name
      @session_date = session.pretty_scheduled_start(true)
      @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session.id}/details"
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html
      end
    end

    def scheduled_session_rescheduled(user, msg, session)
      return if !user.subscribe_email

      @user = user


      email = user.email
      subject = "Session Rescheduled"
      unique_args = {:type => "scheduled_session_rescheduled"}
      @body = msg
      @session_name = session.name
      @session_date = session.pretty_scheduled_start(true)
      @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session.id}/details"
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html
      end
    end

    def scheduled_session_reminder_upcoming(user, session)
      @user = user

      subject = "Your JamKazam session starts in 1 hour!"
      unique_args = {:type => "scheduled_session_reminder_upcoming"}
      send_scheduled_session_reminder(user, session, subject, unique_args)
    end

    def scheduled_session_reminder_day(user, session)
      @user = user

      subject = "JamKazam Session Reminder"
      unique_args = {:type => "scheduled_session_reminder_day"}
      send_scheduled_session_reminder(user, session, subject, unique_args)
    end

    def send_scheduled_session_reminder(user, session, subject, unique_args)
      return if !user.subscribe_email

      email = user.email
      @user = user
      @session_name = session.name
      @session_date = session.pretty_scheduled_start(true)
      @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session.id}/details"
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html
      end
    end

    def scheduled_session_comment(target_user, sender, msg, comment, session)
      return if !target_user.subscribe_email

      @user = target_user


      email = target_user.email
      subject = "New Session Comment"
      unique_args = {:type => "scheduled_session_comment"}
      @body = msg
      @session_name = session.name
      @session_date = session.pretty_scheduled_start(true)
      @comment = comment
      @sender = sender
      @suppress_user_has_account_footer = true
      @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session.id}/details"
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [target_user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def scheduled_session_daily(receiver, sessions_and_latency)
      sendgrid_category "Notification"
      sendgrid_unique_args :type => "scheduled_session_daily"

      sendgrid_recipients([receiver.email])
      sendgrid_substitute('@USERID', [receiver.id])

      @user = receiver
      @sessions_and_latency = sessions_and_latency

      @title = 'New Scheduled Sessions Matched to You'
      mail(:to => receiver.email,
           :subject => EmailBatchScheduledSessions.subject) do |format|
        format.text
        format.html
      end
    end

    def band_session_join(user, msg, session_id)
      return if !user.subscribe_email

      email = user.email
      subject = "A band that you follow has joined a session"
      unique_args = {:type => "band_session_join"}

      @user = user

      @body = msg
      @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session_id}"
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html
      end
    end

    def musician_recording_saved(user, msg)
      return if !user.subscribe_email

      @user = user


      email = user.email
      subject = "A musician has saved a new recording on JamKazam"
      unique_args = {:type => "musician_recording_saved"}

      @body = msg
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html
      end
    end

    def band_recording_saved(user, msg)
      return if !user.subscribe_email

      @user = user


      email = user.email
      subject = "A band has saved a new recording on JamKazam"
      unique_args = {:type => "band_recording_saved"}

      @body = msg
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html
      end
    end

    def band_invitation(user, msg)
      return if !user.subscribe_email

      @user = user

      email = user.email
      subject = "You have been invited to join a band on JamKazam"
      unique_args = {:type => "band_invitation"}

      @body = msg
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html
      end
    end

    def band_invitation_accepted(user, msg)
      return if !user.subscribe_email

      @user = user


      email = user.email
      subject = "Your band invitation was accepted"
      unique_args = {:type => "band_invitation_accepted"}

      @body = msg
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html
      end
    end


    def text_message(user, sender_id, sender_name, sender_photo_url, message)
      return if !user.subscribe_email

      @user = user

      email = user.email
      subject = "Message from #{sender_name}"
      unique_args = {:type => "text_message"}

      @note = message
      @url = Nav.home(dialog: 'text-message', dialog_opts: {d1: sender_id})
      @sender_id = sender_id
      @sender_name = sender_name
      @sender_photo_url = sender_photo_url
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def student_lesson_request(lesson_booking)
      @user = lesson_booking.user
      email = lesson_booking.user.email
      subject = "You have sent a lesson request to #{lesson_booking.teacher.name}!"
      unique_args = {:type => "student_lesson_request"}

      @sender = lesson_booking.teacher
      @lesson_booking = lesson_booking

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [lesson_booking.user.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    # teacher
    def teacher_lesson_request(lesson_booking)
      email = lesson_booking.school_over_teacher
      subject = "You have received a lesson request through JamKazam!"
      unique_args = {:type => "teacher_lesson_request"}

      @sender = lesson_booking.user
      @user = lesson_booking.user
      @lesson_booking = lesson_booking

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients(email)
      sendgrid_substitute('@USERID', lesson_booking.school_over_teacher_ids)

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def student_lesson_accepted(lesson_session, message, slot)
      @slot = slot
      if slot.is_teacher_approved?
        @target = lesson_session.student
        @sender = lesson_session.teacher
        @subject = "Your lesson request is confirmed!"
      else
        @target = lesson_session.teacher
        @sender = lesson_session.student
        @subject = "Your have confirmed a lesson!"
      end
      @lesson_session = lesson_session
      @message = message
      @user = lesson_session.student
      email = lesson_session.student.email
      unique_args = {:type => "student_lesson_accepted"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [lesson_session.student.id])

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def teacher_lesson_accepted(lesson_session, message, slot)
      @slot = slot
      if slot.is_teacher_approved?
        @target = lesson_session.student
        @sender = lesson_session.teacher
        @subject = "You have confirmed a lesson!"
      else
        @target = lesson_session.teacher
        @sender = lesson_session.student
        @subject = "Your lesson time change is confirmed by #{lesson_session.student.name}!"
      end

      @lesson_session = lesson_session
      @message = message
      @user = lesson_session.teacher
      email = lesson_session.school_and_teacher
      unique_args = {:type => "teacher_lesson_accepted"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients(email)
      sendgrid_substitute('@USERID', lesson_session.school_and_teacher_ids)

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def student_lesson_update_all(lesson_session, message, slot)
      @slot = slot
      if slot.is_teacher_created?
        @target = lesson_session.teacher
        @sender = lesson_session.student
        subject = "All lesson times changed with #{lesson_session.teacher.name}!"
      else
        @target = lesson_session.student
        @sender = lesson_session.teacher
        subject = "All lesson times changed with #{lesson_session.teacher.name}!"
      end
      @lesson_session = lesson_session
      @message = message
      @user = lesson_session.student
      email = lesson_session.student.email
      unique_args = {:type => "student_lesson_accepted"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [lesson_session.student.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def teacher_lesson_update_all(lesson_session, message, slot)
      @slot = slot
      if slot.is_teacher_created?
        @target = lesson_session.teacher
        @sender = lesson_session.student
        subject = "All lesson times changed with #{lesson_session.student.name}!"
      else
        @target = lesson_session.student
        @sender = lesson_session.teacher
        subject = "All lesson times changed with #{lesson_session.student.name}!"
      end


      @user = lesson_session.teacher
      @lesson_session = lesson_session
      @message = message
      email = lesson_session.school_and_teacher
      unique_args = {:type => "teacher_lesson_update_all"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients(email)
      sendgrid_substitute('@USERID', lesson_session.school_and_teacher_ids)

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def teacher_scheduled_jamclass_invitation(user, msg, session)

      email = user.email

      @subject = "#{session.lesson_session.lesson_booking.display_type2.capitalize} JamClass Scheduled with #{session.lesson_session.student.name}"
      unique_args = {:type => "scheduled_jamclass_invitation"}
      @user = session.lesson_session.student
      @student = session.lesson_session.student
      @teacher = session.lesson_session.teacher
      @body = msg
      @session_name = session.name
      @session_description = session.description
      @session_date = session.pretty_scheduled_start(true)
      @session_url = session.lesson_session.web_url
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    def student_scheduled_jamclass_invitation(user, msg, session)

      email = user.email
      @subject = "#{session.lesson_session.lesson_booking.display_type2.capitalize} JamClass Scheduled with #{session.lesson_session.teacher.name}"
      unique_args = {:type => "scheduled_jamclass_invitation"}
      @user = session.lesson_session.student
      @student = session.lesson_session.student
      @teacher = session.lesson_session.teacher
      @body = msg
      @session_name = session.name
      @session_description = session.description
      @session_date = session.pretty_scheduled_start(true)
      @session_url = session.lesson_session.web_url
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [user.id])

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    # teacher proposed counter time; so send msg to the student
    def student_lesson_counter(lesson_session, slot)

      @user = lesson_session.student
      email = lesson_session.student.email
      subject = "Instructor has proposed a different time for your lesson"
      unique_args = {:type => "student_lesson_counter"}
      @student = lesson_session.student
      @teacher = lesson_session.teacher
      @session_name = lesson_session.music_session.name
      @session_description = lesson_session.music_session.description
      @session_date = slot.pretty_scheduled_start(true)
      @session_url = lesson_session.web_url
      @lesson_session = lesson_session
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@student.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    # student proposed counter time; so send msg to the teacher
    def teacher_lesson_counter(lesson_session, slot)

      @user = lesson_session.teacher
      email = lesson_session.school_over_teacher
      subject = "Student has proposed a different time for their lesson"
      unique_args = {:type => "teacher_lesson_counter"}
      @student = lesson_session.student
      @teacher = lesson_session.teacher
      @session_name = lesson_session.music_session.name
      @session_description = lesson_session.music_session.description
      @session_date = slot.pretty_scheduled_start(true)
      @session_url = lesson_session.web_url
      @lesson_session = lesson_session
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients(email)
      sendgrid_substitute('@USERID', lesson_session.school_over_teacher_ids)

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def student_lesson_counter_recurring(lesson_booking, slot)

      @user = lesson_booking.student
      email = lesson_booking.student.email
      subject = "Instructor has proposed a different time for all lessons"
      unique_args = {:type => "student_lesson_counter_recurring"}
      @student = lesson_booking.student
      @teacher = lesson_booking.teacher
      @lesson_booking = lesson_booking
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@student.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def teacher_lesson_counter_recurring(lesson_booking, slot)

      @user = lesson_booking.teacher
      email = lesson_booking.school_over_teacher
      subject = "Student has proposed a different time for all lessons"
      unique_args = {:type => "teacher_lesson_counter"}
      @student = lesson_booking.student
      @teacher = lesson_booking.teacher
      @lesson_booking = lesson_booking
      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients(email)
      sendgrid_substitute('@USERID', lesson_booking.school_over_teacher_ids)

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def teacher_lesson_completed(lesson_session)
      @student = lesson_session.student
      @teacher = lesson_session.teacher
      @session_name = lesson_session.music_session.name
      @session_description = lesson_session.music_session.description
      @session_date = lesson_session.slot.pretty_scheduled_start(true)
      @session_url = lesson_session.web_url
      @lesson_session = lesson_session
      @user = lesson_session.teacher
      email = lesson_session.school_and_teacher
      if @lesson_session.student_missed
        subject = "You will be paid for your lesson with #{@student.name}"
      else
        subject = "You successfully completed a lesson with #{@student.name}"
      end


      unique_args = {:type => "teacher_lesson_completed"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients(email)
      sendgrid_substitute('@USERID', lesson_session.school_and_teacher_ids)

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    # successfully completed, and has some remaining test drives
    def student_test_drive_lesson_completed(lesson_session)

      @student = lesson_session.student
      @teacher = lesson_session.teacher
      @session_name = lesson_session.music_session.name
      @session_description = lesson_session.music_session.description
      @session_date = lesson_session.slot.pretty_scheduled_start(true)
      @session_url = lesson_session.web_url
      @lesson_session = lesson_session

      @user = lesson_session.student
      email = @student.email


      if lesson_session.posa_card
        @total_credits = @student.total_posa_credits
        @used_credits = @student.used_posa_credits
        @remaining_credits = @student.jamclass_credits
      else
        @total_credits = @student.total_test_drives
        @used_credits = @student.used_test_drives
        @remaining_credits = @student.remaining_test_drives
      end

      subject = "You have used #{@used_credits} of #{@total_credits} TestDrive lesson credits"

      unique_args = {:type => "student_test_drive_success"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@student.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def teacher_test_drive_no_bill(lesson_session)
      @user = lesson_session.teacher
      @student = lesson_session.student
      @teacher = lesson_session.teacher
      @session_name = lesson_session.music_session.name
      @session_description = lesson_session.music_session.description
      @session_date = lesson_session.slot.pretty_scheduled_start(true)
      @session_url = lesson_session.web_url
      @lesson_session = lesson_session

      email = lesson_session.school_and_teacher
      subject = "Your TestDrive with #{@student.name} was not successful"
      unique_args = {:type => "teacher_test_drive_no_bill"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients(email)
      sendgrid_substitute('@USERID', lesson_session.school_and_teacher_ids)

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def student_test_drive_no_bill(lesson_session)
      @user = lesson_session.student
      @student = lesson_session.student
      @teacher = lesson_session.teacher
      @session_name = lesson_session.music_session.name
      @session_description = lesson_session.music_session.description
      @session_date = lesson_session.slot.pretty_scheduled_start(true)
      @session_url = lesson_session.web_url
      @lesson_session = lesson_session

      email = @student.email
      subject = "Your TestDrive with #{@teacher.name} will not use a credit"
      unique_args = {:type => "student_test_drive_no_bill"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@student.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    # successfully completed, but no more test drives left
    def student_test_drive_lesson_done(lesson_session)

      @user = lesson_session.student
      @student = lesson_session.student
      @teacher = lesson_session.teacher
      @session_name = lesson_session.music_session.name
      @session_description = lesson_session.music_session.description
      @session_date = lesson_session.slot.pretty_scheduled_start(true)
      @session_url = lesson_session.web_url
      @lesson_session = lesson_session

      email = @student.email
      subject = "You have used all TestDrive lesson credits"
      unique_args = {:type => "student_test_drive_success"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@student.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "raw_mailer" }
      end
    end

    def student_lesson_normal_no_bill(lesson_session)
      @user = lesson_session.student
      @student = lesson_session.student
      @teacher = lesson_session.teacher
      @session_name = lesson_session.music_session.name
      @session_description = lesson_session.music_session.description
      @session_date = lesson_session.slot.pretty_scheduled_start(true)
      @session_url = lesson_session.web_url
      @lesson_session = lesson_session

      email = @student.email
      subject = "Your lesson with #{@teacher.name} will not be billed"
      unique_args = {:type => "student_lesson_normal_no_bill"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@student.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def teacher_lesson_normal_no_bill(lesson_session)
      @user = lesson_session.teacher
      @student = lesson_session.student
      @teacher = lesson_session.teacher
      @session_name = lesson_session.music_session.name
      @session_description = lesson_session.music_session.description
      @session_date = lesson_session.slot.pretty_scheduled_start(true)
      @session_url = lesson_session.web_url
      @lesson_session = lesson_session
      email = lesson_session.school_and_teacher
      subject = "Your student #{@student.name} will not be charged for their lesson"
      unique_args = {:type => "teacher_lesson_normal_no_bill"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients(email)
      sendgrid_substitute('@USERID', lesson_session.school_and_teacher_ids)

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def student_lesson_normal_done(lesson_session)
      @user = lesson_session.student
      @student = lesson_session.student
      @teacher = lesson_session.teacher
      @session_name = lesson_session.music_session.name
      @session_description = lesson_session.music_session.description
      @session_date = lesson_session.slot.pretty_scheduled_start(true)
      @session_url = lesson_session.web_url
      @lesson_session = lesson_session

      email = @student.email
      subject = "Your JamClass lesson today with #{@teacher.first_name}"
      unique_args = {:type => "student_lesson_normal_done"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@student.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def teacher_lesson_normal_done(lesson_session)
      @user = lesson_session.teacher
      @student = lesson_session.student
      @teacher = lesson_session.teacher
      @session_name = lesson_session.music_session.name
      @session_description = lesson_session.music_session.description
      @session_date = lesson_session.slot.pretty_scheduled_start(true)
      @session_url = lesson_session.web_url
      @lesson_session = lesson_session
      email = lesson_session.school_over_teacher
      subject = "Your JamClass lesson today with #{@student.first_name}"
      unique_args = {:type => "teacher_lesson_normal_done"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients(email)
      sendgrid_substitute('@USERID', lesson_session.school_over_teacher_ids)

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def student_unable_charge(lesson_session)
      @user = lesson_session.student
      @student = lesson_session.student
      @teacher = lesson_session.teacher
      @session_name = lesson_session.music_session.name
      @session_description = lesson_session.music_session.description
      @session_date = lesson_session.slot.pretty_scheduled_start(true)
      @session_url = lesson_session.web_url
      @lesson_session = lesson_session
      @card_declined = lesson_session.is_card_declined?
      @card_expired = lesson_session.is_card_expired?
      @bill_date = lesson_session.last_billed_at_date

      email = @student.email
      subject = "The credit card charge for your lesson today with #{@teacher.name} failed"
      unique_args = {:type => "student_unable_charge"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@student.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def teacher_unable_charge(lesson_session)
      @user = lesson_session.teacher
      @student = lesson_session.student
      @teacher = lesson_session.teacher
      @session_name = lesson_session.music_session.name
      @session_description = lesson_session.music_session.description
      @session_date = lesson_session.slot.pretty_scheduled_start(true)
      @session_url = lesson_session.web_url
      @lesson_session = lesson_session
      email = lesson_session.teacher.email
      subject = "The credit card charge for your lesson today with #{@teacher.name} failed"
      unique_args = {:type => "teacher_lesson_normal_done"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@teacher.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def student_unable_charge_monthly(lesson_package_purchase)
      lesson_booking = lesson_package_purchase.lesson_booking
      @user = lesson_booking.student
      @student = lesson_booking.student
      @teacher = lesson_booking.teacher
      @lesson_package_purchase = lesson_package_purchase
      @card_declined = lesson_package_purchase.is_card_declined?
      @card_expired = lesson_package_purchase.is_card_expired?
      @bill_date = lesson_package_purchase.last_billed_at_date
      @lesson_booking = lesson_booking
      @month_name = lesson_package_purchase.month_name
      email = @student.email
      if lesson_booking.is_suspended?
        @subject = "Your weekly lessons with #{@teacher.name} have been suspended."
      else
        @subject = "The credit card charge for your #{@month_name} lessons with #{@teacher.name} failed."
      end


      unique_args = {:type => "student_unable_charge_monthly"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@student.id])

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def teacher_unable_charge_monthly(lesson_package_purchase)
      lesson_booking = lesson_package_purchase.lesson_booking
      @user = lesson_booking.teacher
      @student = lesson_booking.student
      @teacher = lesson_booking.teacher
      @lesson_package_purchase = lesson_package_purchase
      @card_declined = lesson_package_purchase.is_card_declined?
      @card_expired = lesson_package_purchase.is_card_expired?
      @bill_date = lesson_package_purchase.last_billed_at_date
      @lesson_booking = lesson_booking
      @month_name = lesson_package_purchase.month_name

      email = lesson_booking.school_over_teacher
      if lesson_booking.is_suspended?
        @subject = "Your weekly lessons with #{@student.name} has been suspended."
      else
        @subject = "The student #{@student.name} had a failed credit card charge for #{@month_name}."
      end


      unique_args = {:type => "teacher_unable_charge_monthly"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients(email)
      sendgrid_substitute('@USERID', lesson_booking.school_over_teacher_ids)

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def student_lesson_monthly_charged(lesson_package_purchase)
      lesson_booking = lesson_package_purchase.lesson_booking
      @user = lesson_booking.student
      @student = lesson_booking.student
      @teacher = lesson_booking.teacher
      @lesson_package_purchase = lesson_package_purchase
      @card_declined = lesson_package_purchase.is_card_declined?
      @card_expired = lesson_package_purchase.is_card_expired?
      @bill_date = lesson_package_purchase.last_billed_at_date
      @lesson_booking = lesson_booking
      @month_name = lesson_package_purchase.month_name
      email = @student.email
      @subject = "Your JamClass lessons with #{@teacher.first_name} for #{@month_name}"

      unique_args = {:type => "student_lesson_monthly_charged"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@student.id])

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def teacher_lesson_monthly_charged(lesson_package_purchase)
      lesson_booking = lesson_package_purchase.lesson_booking
      @user = lesson_booking.teacher
      @student = lesson_booking.student
      @teacher = lesson_booking.teacher
      @lesson_package_purchase = lesson_package_purchase
      @card_declined = lesson_package_purchase.is_card_declined?
      @card_expired = lesson_package_purchase.is_card_expired?
      @bill_date = lesson_package_purchase.last_billed_at_date
      @lesson_booking = lesson_booking
      @month_name = lesson_package_purchase.month_name

      email = lesson_booking.school_over_teacher
      if lesson_booking.is_suspended?
        @subject = "Your weekly lessons with #{@student.name} has been suspended."
      else
        @subject = "The student #{@student.name} had a failed credit card charge for #{@month_name}."
      end


      unique_args = {:type => "student_lesson_monthly_charged"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients(email)
      sendgrid_substitute('@USERID', lesson_booking.school_over_teacher_ids)

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    # always goes to the teacher
    def teacher_distribution_done(teacher_payment)
      @school = teacher_payment.school
      @teacher_payment = teacher_payment
      @distribution = teacher_payment.teacher_distribution
      @teacher = teacher_payment.teacher
      @payable_teacher = teacher_payment.payable_teacher
      @name = @teacher.first_name || 'Anonymous'
      @student = @distribution.student
      email = @distribution.target.lesson_booking.school_over_teacher
      @user = @teacher

      if @school
        if @distribution.is_test_drive?
          @subject = "Your TestDrive lesson with #{@student.name}"
        elsif @distribution.is_normal?
          @subject = "Your lesson with #{@student.name}"
        elsif @distribution.is_monthly?
          @subject = "Your #{@distribution.month_name} lessons with #{@student.name}"
        else
          @subject = "Your lesson with #{@student.name}"
        end
      else
        if @distribution.is_test_drive?
          @subject = "You have earned #{@distribution.real_distribution_display} for your TestDrive lesson with #{@student.first_name}"
        elsif @distribution.is_normal?
          @subject = "You have earned #{@distribution.real_distribution_display} for your lesson with #{@student.first_name}"
        elsif @distribution.is_monthly?
          @subject = "You have earned #{@distribution.real_distribution_display} for your #{@distribution.month_name} lessons with #{@student.first_name}"
        else
          @subject = "You have earned #{@distribution.real_distribution_display} for your lesson with #{@student.first_name}"
        end
      end

      unique_args = {:type => "teacher_distribution_done"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients(email)
      sendgrid_substitute('@USERID',@distribution.target.lesson_booking.school_over_teacher_ids )

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    # if school, goes to school owner; otherwise goes to teacher
    def teacher_distribution_fail(teacher_payment)
      @school = teacher_payment.school
      @teacher_payment = teacher_payment
      @distribution = teacher_payment.teacher_distribution
      @teacher = teacher_payment.teacher
      @payable_teacher = teacher_payment.payable_teacher
      @student = @distribution.student
      @name = @payable_teacher.first_name || 'Anonymous'
      email = @distribution.target.lesson_booking.school_over_teacher
      @user = @teacher

      @card_declined = teacher_payment.is_card_declined?
      @card_expired = teacher_payment.is_card_expired?
      @bill_date = teacher_payment.last_billed_at_date


      if @school
        if @distribution.is_test_drive?
          @subject = "We had a problem paying #{@distribution.real_distribution_display} for #{@teacher.name}'s TestDrive lesson with #{@student.name}"
        elsif @distribution.is_normal?
          @subject = "We had a problem paying #{@distribution.real_distribution_display} for #{@teacher.name}'s lesson with #{@student.name}"
        elsif @distribution.is_monthly?
          @subject = "We had a problem paying #{@distribution.real_distribution_display} for #{@teacher.name}'s #{@distribution.month_name} lessons with #{@student.name}"
        else
          @subject = "We had a problem paying #{@distribution.real_distribution_display} for #{@teacher.name}'s lesson with #{@student.name}"
        end
      else
        if @distribution.is_test_drive?
          @subject = "We had a problem paying you #{@distribution.real_distribution_display} for your TestDrive lesson with #{@student.first_name}"
        elsif @distribution.is_normal?
          @subject = "We had a problem paying you #{@distribution.real_distribution_display} for your lesson with #{@student.first_name}"
        elsif @distribution.is_monthly?
          @subject = "We had a problem paying you #{@distribution.real_distribution_display} for your #{@distribution.month_name} lessons with #{@student.first_name}"
        else
          @subject = "We had a problem paying you #{@distribution.real_distribution_display} for your lesson with #{@student.first_name}"
        end
      end

      unique_args = {:type => "teacher_distribution_fail"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients(email)
      sendgrid_substitute('@USERID', @distribution.target.lesson_booking.school_over_teacher_ids)

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    # always goes to the school owner
    def school_distribution_done(teacher_payment)
      @school = teacher_payment.school
      @teacher_payment = teacher_payment
      @distribution = teacher_payment.teacher_distribution
      @teacher = teacher_payment.teacher
      @payable_teacher = @school.owner
      @name = @payable_teacher.first_name || 'Anonymous'
      @student = @distribution.student
      @user = @teacher
      email = @payable_teacher.email

      if @distribution.is_test_drive?
        @subject = "#{@teacher.name} has earned #{@distribution.real_distribution_display} for TestDrive lesson with #{@student.name}"
      elsif @distribution.is_normal?
        @subject = "#{@teacher.name} has earned #{@distribution.real_distribution_display} for a lesson with #{@student.name}"
      elsif @distribution.is_monthly?
        @subject = "#{@teacher.name} has earned #{@distribution.real_distribution_display} for #{@distribution.month_name} lessons with #{@student.name}"
      else
        @subject = "#{@teacher.name} has earned #{@distribution.real_distribution_display} for a lesson with #{@student.name}"
      end

      unique_args = {:type => "school_distribution_done"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@payable_teacher.id])

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end


    def monthly_recurring_done(lesson_session)
      @student = lesson_session.student
      @teacher = lesson_session.teacher
      @session_name = lesson_session.music_session.name
      @session_description = lesson_session.music_session.description
      @session_date = lesson_session.slot.pretty_scheduled_start(true)
      @session_url = lesson_session.web_url
      @lesson_session = lesson_session
      @user = @student

      email = @student.email
      subject = "Your JamClass lesson today with #{@teacher.first_name}"
      unique_args = {:type => "monthly_recurring_done"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@student.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def monthly_recurring_no_bill(lesson_session)
      @student = lesson_session.student
      @teacher = lesson_session.teacher
      @session_name = lesson_session.music_session.name
      @session_description = lesson_session.music_session.description
      @session_date = lesson_session.slot.pretty_scheduled_start(true)
      @session_url = lesson_session.web_url
      @lesson_session = lesson_session
      @user = @student

      email = @student.email
      subject = "Your lesson with #{@teacher.name} will not be billed"
      unique_args = {:type => "student_lesson_normal_done"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@student.id])

      mail(:to => email, :subject => subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def student_lesson_booking_declined(lesson_booking, message)
      @lesson_booking = lesson_booking
      @student = lesson_booking.student
      @teacher = lesson_booking.teacher
      @message = message
      @lesson_session = lesson_booking.next_lesson
      @session_name = @lesson_session.music_session.name
      @session_description = @lesson_session.music_session.description
      @session_date = @lesson_session.slot.pretty_scheduled_start(true)
      email = @student.email
      @user = @student
      @subject = "We're sorry your lesson request has been declined"
      unique_args = {:type => "student_lesson_booking_declined"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]
      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@student.id])

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def student_lesson_booking_canceled(lesson_booking, message)
      @lesson_booking = lesson_booking
      @student = lesson_booking.student
      @teacher = lesson_booking.teacher
      @message = message
      @lesson_session = lesson_booking.next_lesson
      @session_name = @lesson_session.music_session.name
      @session_description = @lesson_session.music_session.description
      @session_date = @lesson_session.slot.pretty_scheduled_start(true)
      email = @student.email
      @user = @student
      @subject = "Your lesson has been canceled"
      unique_args = {:type => "student_lesson_booking_canceled"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]
      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@student.id])

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def teacher_lesson_booking_canceled(lesson_booking, message)
      @lesson_booking = lesson_booking
      @student = lesson_booking.student
      @teacher = lesson_booking.teacher
      @message = message
      @lesson_session = lesson_booking.next_lesson
      @session_name = @lesson_session.music_session.name
      @session_description = @lesson_session.music_session.description
      @session_date = @lesson_session.slot.pretty_scheduled_start(true)
      @user = @teacher
      email = @lesson_booking.school_and_teacher
      @subject = "Your lesson has been canceled"
      unique_args = {:type => "teacher_lesson_booking_canceled"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]
      sendgrid_recipients(email)
      sendgrid_substitute('@USERID', @lesson_booking.school_and_teacher_ids)

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def student_lesson_canceled(lesson_session, message)
      @lesson_booking = lesson_booking = lesson_session.lesson_booking
      @student = lesson_booking.student
      @teacher = lesson_booking.teacher
      @message = message
      @lesson_session = lesson_booking.next_lesson
      @session_name = @lesson_session.music_session.name
      @session_description = @lesson_session.music_session.description
      @session_date = @lesson_session.slot.pretty_scheduled_start(true)
      email = @student.email
      @user = @student
      @subject = "Your lesson has been canceled"
      unique_args = {:type => "student_lesson_canceled"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]
      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@student.id])

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def teacher_lesson_canceled(lesson_session, message)
      @lesson_booking = lesson_booking = lesson_session.lesson_booking
      @student = lesson_booking.student
      @teacher = lesson_booking.teacher
      @message = message
      @lesson_session = lesson_booking.next_lesson
      @session_name = @lesson_session.music_session.name
      @session_description = @lesson_session.music_session.description
      @session_date = @lesson_session.slot.pretty_scheduled_start(true)

      @user = @teacher
      email = @lesson_booking.school_and_teacher
      @subject = "Your lesson has been canceled"
      unique_args = {:type => "teacher_lesson_canceled"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]
      sendgrid_recipients(email)
      sendgrid_substitute('@USERID', @lesson_booking.school_and_teacher_ids)

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def invite_retailer_teacher(retailer_invitations)
      @retailer_invitation = retailer_invitations
      @retailer = retailer_invitations.retailer

      email = retailer_invitations.email
      @subject = "#{@retailer.owner.name} has sent you an invitation to join #{@retailer.name} on JamKazam"
      unique_args = {:type => "invite_retailer_teacher"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]
      sendgrid_recipients([email])

      @suppress_user_has_account_footer = true

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    def invite_school_teacher(school_invitation)
      @school_invitation = school_invitation
      @school = school_invitation.school

      email = school_invitation.email
      @subject = "#{@school.owner.name} has sent you an invitation to join #{@school.name} on JamKazam"
      unique_args = {:type => "invite_school_teacher"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]
      sendgrid_recipients([email])

      @suppress_user_has_account_footer = true

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    def invite_school_student(school_invitation)
      @school_invitation = school_invitation
      @school = school_invitation.school

      email = school_invitation.email
      @subject = "#{@school.name} has sent you an invitation to join JamKazam for lessons"
      unique_args = {:type => "invite_school_student"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]
      sendgrid_recipients([email])

      @suppress_user_has_account_footer = true

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    def lesson_chat(chat_msg)
      @target = chat_msg.target_user
      @sender = chat_msg.user
      @message = chat_msg.message
      @lesson_session = chat_msg.lesson_session
      @session_name = @lesson_session.music_session.name
      @session_description = @lesson_session.music_session.description
      @session_date = @lesson_session.slot.pretty_scheduled_start(true)
      @user = @target
      if chat_msg.user == chat_msg.lesson_session.student
        email = @lesson_session.school_over_teacher
      else
        email = [chat_msg.lesson_session.student.email]
      end

      @subject = "#{@sender.name} has sent you a message about a lesson"
      unique_args = {:type => "lesson_chat"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]
      sendgrid_recipients(email)
      sendgrid_substitute('@USERID', @lesson_session.school_over_teacher_ids)

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    # please respond to outstanding counter!
    def student_counter_reminder(lesson_session)
      @student = lesson_session.student
      @teacher = lesson_session.teacher
      @session_url = lesson_session.web_url
      @lesson_session = lesson_session
      @user = @student
      email = @student.email
      @subject = "Instructor's time proposal is still awaiting your response"
      unique_args = {:type => "student_counter_reminder"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@student.id])

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    # please respond to outstanding counter!
    def teacher_counter_reminder(lesson_session)
      @student = lesson_session.student
      @teacher = lesson_session.teacher
      @session_name = lesson_session.music_session.name
      @session_description = lesson_session.music_session.description
      @session_date = lesson_session.slot.pretty_scheduled_start(true)
      @session_url = lesson_session.web_url
      @lesson_session = lesson_session
      @user = @teacher
      email = lesson_session.school_over_teacher
      @subject = "Student #{@student.name}'s time proposal is still awaiting your response"
      unique_args = {:type => "teacher_counter_reminder"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]

      sendgrid_recipients(email)
      sendgrid_substitute('@USERID', lesson_session.school_over_teacher_ids)

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end


    def lesson_starting_soon_teacher(lesson_session, early_notice, time_stmt)
      @early_notice = early_notice
      @lesson_booking = lesson_booking = lesson_session.lesson_booking
      @student = lesson_booking.student
      @teacher = lesson_booking.teacher
      @lesson_session = lesson_booking.next_lesson
      @session_name = @lesson_session.music_session.name
      @session_description = @lesson_session.music_session.description
      @session_date = @lesson_session.slot.pretty_scheduled_start(true)
      if @student.anonymous?
        @student_name = 'a student'
      else
        @student_name = @student.name
      end

      @user = @teacher
      email = @teacher.email
      if early_notice
        @subject = "Your JamKazam lesson starts in 2 hours at #{time_stmt}"
      else
        @subject = "Your JamKazam lesson starts in 30 minutes at #{time_stmt}"
      end

      unique_args = {:type => "send_starting_notice_teacher"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]
      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@teacher.id])

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def lesson_starting_soon_student(lesson_session, early_notice, time_stmt)
      @early_notice = early_notice
      @lesson_booking = lesson_booking = lesson_session.lesson_booking
      @student = lesson_booking.student
      @teacher = lesson_booking.teacher
      @message = message
      @lesson_session = lesson_booking.next_lesson
      @session_name = @lesson_session.music_session.name
      @session_description = @lesson_session.music_session.description
      @session_date = @lesson_session.slot.pretty_scheduled_start(true)
      @user = @student
      if @student.anonymous?
        @student_name = 'a student'
      else
        @student_name = @student.name
      end
      email = @student.email
      if early_notice
        @subject = "Your JamKazam lesson starts in 2 hours at #{time_stmt}"
      else
        @subject = "Your JamKazam lesson starts in 30 minutes at #{time_stmt}"
      end

      unique_args = {:type => "send_starting_notice_student"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]
      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@student.id])

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def lesson_attachment(sender, target, lesson_session, attachment)
      @sender = sender
      @target = target
      @lesson_session = lesson_session
      @attachment = attachment

      @user = target
      email = target.email
      @subject = "An attachment has been added to your lesson by #{sender.name}"
      unique_args = {:type => "lesson_attachment"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]
      sendgrid_recipients([email])
      sendgrid_substitute('@USERID', [@target.id])

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html { render :layout => "from_user_mailer" }
      end
    end

    def retailer_customer_blast(email, retailer)
      @retailer = retailer
      @subject = "Check out our teachers at #{@retailer.name}"
      unique_args = {:type => "retailer_customer_email"}

      sendgrid_category "Notification"
      sendgrid_unique_args :type => unique_args[:type]
      sendgrid_recipients([email])

      @suppress_user_has_account_footer = true
      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    def amazon_prompt_take_lesson(user, teacher, attempt)

      @user = user
      @attempt = attempt
      @teacher = teacher
      @biography = teacher.teacher.biography
      if @biography.nil?
        @biography = ''
      else
        @biography = @biography.truncate(160)
      end


      email = user.email
      if attempt == 0
        @subject = "Scheduling your first free lesson"
      elsif attempt == 1
        @subject = "Don't forget to book your first free lesson"
      else
        @subject = "Last reminder to schedule your first free lesson!"
      end
      @book_lesson_link = "#{APP_CONFIG.external_root_url}/client#/jamclass/book-lesson/test-drive_#{@teacher.id}"


      unique_args = {:type => "amazon_prompt_take_lesson"}

      sendgrid_category "amazon_prompt_take_lesson"
      sendgrid_unique_args :type => unique_args[:type]
      sendgrid_recipients([email])

      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html {render :layout => "raw_mailer"}
      end
    end

    def amazon_first_lesson_scheduled(user)
      @user = user
      @subject = "IMPORTANT - instructions to be ready for your first lesson!"
      email = user.email

      unique_args = {:type => "amazon_first_lesson_scheduled"}

      sendgrid_category "amazon_first_lesson_scheduled"
      sendgrid_unique_args :type => unique_args[:type]
      sendgrid_recipients([email])
      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end

    def amazon_first_lesson_instructions(user)
      @user = user
      @subject = "How to join your first lesson"
      email = user.email

      unique_args = {:type => "amazon_first_lesson_instructions"}

      sendgrid_category "amazon_first_lesson_instructions"
      sendgrid_unique_args :type => unique_args[:type]
      sendgrid_recipients([email])
      mail(:to => email, :subject => @subject) do |format|
        format.text
        format.html
      end
    end
  end
end
