class ErrorReportMailer < ActionMailer::Base
  default to: 'devteam@annarbortees.com'

  def send_report(user, params)
    @user  = user || User.find_by(id: params[:user_id])
    if ["Softwear CRM", "Softwear Production"].include?(app_name)
      @order = Order.find_by(id: params[:order_id])

      if @user.nil?
        from_customer = true

        if @order.nil?
          @user = OpenStruct.new(
            email:     'unknown-user@annarbortees.com',
            full_name: 'Unknown Customer'
          )
        else
          @user = OpenStruct.new(
            email:     @order.email,
            full_name: "(Customer) #{@order.full_name}"
          )
        end
      end
      
    end
    
    if @user.nil?
      @user = OpenStruct.new(
        email:     'unknown-user@annarbortees.com',
        full_name: 'Unknown Customer'
      )
    end

    @error_class     = params[:error_class]
    @error_message   = params[:error_message]
    @backtrace       = params[:backtrace]
    @user_message    = params[:user_message]
    @additional_info = params[:additional_info]

    mail(
      from:     from_customer ? 'customer-report@softwearcrm.com' : @user.email,
      reply_to: @user.email,
      to:       'devteam@annarbortees.com',
      subject:  "#{app_name} Error Report from #{@user.full_name}"
    )
  end

  def auth_server_down(at)
    mail(
      from:    'noreply@softwearcrm.com',
      subject: 'Authentication server is down!',
      body:    at.strftime("Lost contact on %m/%d/%Y at %I:%M%p. We're running on cache!")
    )
  end

  def auth_server_up(at)
    mail(
      from:    'noreply@softwearcrm.com',
      subject: 'Authentication server back up!',
      body:    at.strftime("Just got a response at %I:%M%p")
    )
  end
 
  private 

  def app_name
    case Rails.application.class.parent.to_s
    when "CrmSoftwearcrmCom"  then return "Softwear CRM"
    when "RetailPublishing"   then return "Softwear Mockbot"
    when "SoftwearProduction" then return "Softwear Production"
    when "SoftwearHub"        then return "Softwear Hub"
    when "AnnarborteesFba"    then return "Softwear Fba"
    when "AnnarborteesWww"    then return "Ann Arbor Tees WWW"
    else
      return "Unknown App"
    end  
  end

end