defmodule Microwaveprop.Accounts.UserNotifier do
  @moduledoc false
  import Swoosh.Email

  alias Microwaveprop.Mailer

  # Delivers the email using the application mailer.
  defp deliver(recipient, subject, body) do
    email =
      new()
      |> to(recipient)
      |> Mailer.apply_defaults()
      |> subject(subject)
      |> text_body(body)

    with {:ok, _metadata} <- Mailer.deliver(email) do
      {:ok, email}
    end
  end

  @doc """
  Deliver instructions to confirm a newly registered account.
  """
  def deliver_confirmation_instructions(user, url) do
    deliver(user.email, "Confirm your NTMS Propagation account", """

    ==============================

    Hi #{user.callsign},

    Welcome to the NTMS Propagation tool. To finish creating your
    account, please confirm your email by visiting the URL below:

    #{url}

    This link expires in 24 hours. If you didn't create this account,
    please ignore this message.

    ==============================
    """)
  end

  @doc """
  Deliver instructions to reset a forgotten password.
  """
  def deliver_reset_password_instructions(user, url) do
    deliver(user.email, "Reset your NTMS Propagation password", """

    ==============================

    Hi #{user.callsign},

    Someone requested a password reset for your NTMS Propagation
    account. To pick a new password, visit the URL below:

    #{url}

    This link expires in 24 hours. If you didn't request this,
    please ignore this message — your password won't change.

    ==============================
    """)
  end

  @doc """
  Deliver instructions to update a user email.
  """
  def deliver_update_email_instructions(user, url) do
    deliver(user.email, "Update email instructions", """

    ==============================

    Hi #{user.email},

    You can change your email by visiting the URL below:

    #{url}

    If you didn't request this change, please ignore this.

    ==============================
    """)
  end
end
