defmodule Mv.Accounts.User.Senders.SendPasswordResetEmail do
  @moduledoc """
  Sends a password reset email
  """

  use AshAuthentication.Sender
  use MvWeb, :verified_routes

  import Swoosh.Email

  alias Mv.Mailer

  @doc """
  Sends a password reset email to a user.

  This function is called automatically by AshAuthentication when a user
  requests a password reset.

  ## Parameters
  - `user` - The user record requesting the password reset
  - `token` - The password reset 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("Reset your password")
    |> html_body(body(token: token))
    |> Mailer.deliver!()
  end

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

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