
class ApiAlertsController < ApiController

  before_filter :api_signed_in_user, only: :create


  def log
    @log || Logging.logger[ApiAlertsController]
  end


  def create

    if Rails.application.config.alerts_api_enabled

      result = params[:result]
      detail = params[:detail]

      body = ""
      body << "User: " + current_user.admin_url + "\n"
      body << "Reason: #{result}\n" if result
      body << "Detail: #{detail}\n" if detail
      body << "\n\n\ndata:\n-----\n"
      body << JSON.pretty_generate(params)

        AdminMailer.alerts({
                               subject:params[:subject] || 'Alerts API (no subject)',
                               body:body
                           }).deliver_now


    end

    render json: {}, :status => :ok


  end

  def bad_audio

    subject = "Bad Audio"
    body = header
    if current_user
      subject + " - #{current_user.name}"
      body << "<h1>User - #{current_user.name} - #{current_user.email}</h1>"
    end


    #body << params.to_yaml

    body << "<br/><br/>"
    body << "<pre>"
    body << JSON.pretty_generate(params["api_alert"])
    body << "</pre>"
    AdminMailer.pretty({ to: Rails.application.config.bugs_alias,
                           subject:params[:subject] || subject,
                           body:body
                       }).deliver_now


    render json: {}, :status => :ok

  end

  private
  def header
    "<html><body>"
  end
  def footer
    "</body></html>"
  end
end
