defmodule Mv.Accounts.User.Senders.SendNewUserConfirmationEmail do
  @moduledoc """
  Sends an email for a new user to confirm their email address.
  """

  use AshAuthentication.Sender
  use MvWeb, :verified_routes

  import Swoosh.Email

  alias Mv.Mailer

  @doc """
  Sends a confirmation email to a new user.

  This function is called automatically by AshAuthentication when a new
  user registers and needs to confirm their email address.

  ## Parameters
  - `user` - The user record who needs to confirm their email
  - `token` - The confirmation token to include in the email link
  - `_opts` - Additional options (unused)

  ## Returns
  The Swoosh.Email delivery result from `Mailer.deliver!/1`.
  """
  @impl true
  def send(user, token, _) do
    new()
    # Replace with email from env
    |> from({"noreply", "noreply@example.com"})
    |> to(to_string(user.email))
    |> subject("Confirm your email address")
    |> html_body(body(token: token))
    |> Mailer.deliver!()
  end

  defp body(params) do
    url = url(~p"/confirm_new_user/#{params[:token]}")

    """
    <p>Click this link to confirm your email:</p>
    <p><a href="#{url}">#{url}</a></p>
    """
  end
end
