defmodule SimpleshopTheme.Orders.OrderNotifier do
  @moduledoc """
  Sends transactional emails for orders.

  Order confirmation after payment, shipping notification when dispatched.
  """

  import Swoosh.Email

  alias SimpleshopTheme.Cart
  alias SimpleshopTheme.Mailer

  require Logger

  @doc """
  Sends an order confirmation email after successful payment.

  Skips silently if the order has no customer email.
  """
  def deliver_order_confirmation(%{customer_email: nil}), do: {:ok, :no_email}
  def deliver_order_confirmation(%{customer_email: ""}), do: {:ok, :no_email}

  def deliver_order_confirmation(order) do
    subject = "Order confirmed - #{order.order_number}"

    body = """
    ==============================

    Thanks for your order!

    Order: #{order.order_number}

    #{format_items(order.items)}
    Total: #{Cart.format_price(order.total)}

    #{format_shipping_address(order.shipping_address)}
    We'll send you another email when your order ships.

    ==============================
    """

    deliver(order.customer_email, subject, body)
  end

  @doc """
  Sends a shipping notification with tracking info.

  Skips silently if the order has no customer email.
  """
  def deliver_shipping_notification(%{customer_email: nil}), do: {:ok, :no_email}
  def deliver_shipping_notification(%{customer_email: ""}), do: {:ok, :no_email}

  def deliver_shipping_notification(order) do
    subject = "Your order has shipped - #{order.order_number}"

    body = """
    ==============================

    Good news! Your order #{order.order_number} is on its way.

    #{format_tracking(order)}
    Thanks for shopping with us.

    ==============================
    """

    deliver(order.customer_email, subject, body)
  end

  # --- Private ---

  defp deliver(recipient, subject, body) do
    email =
      new()
      |> to(recipient)
      |> from({"SimpleshopTheme", "contact@example.com"})
      |> subject(subject)
      |> text_body(body)

    case Mailer.deliver(email) do
      {:ok, _metadata} = result ->
        result

      {:error, reason} = error ->
        Logger.warning("Failed to send email to #{recipient}: #{inspect(reason)}")
        error
    end
  end

  defp format_items(items) when is_list(items) do
    items
    |> Enum.map_join("\n", fn item ->
      price = Cart.format_price(item.unit_price * item.quantity)
      "  #{item.quantity}x #{item.product_name} (#{item.variant_title}) - #{price}"
    end)
  end

  defp format_items(_), do: ""

  defp format_shipping_address(address) when is_map(address) and map_size(address) > 0 do
    lines =
      [
        address["name"],
        address["line1"],
        address["line2"],
        [address["city"], address["postal_code"]] |> Enum.reject(&is_nil/1) |> Enum.join(" "),
        address["state"],
        address["country"]
      ]
      |> Enum.reject(&(is_nil(&1) or &1 == ""))
      |> Enum.map_join("\n", &"  #{&1}")

    "Shipping to:\n#{lines}\n\n"
  end

  defp format_shipping_address(_), do: ""

  defp format_tracking(order) do
    cond do
      order.tracking_url not in [nil, ""] and order.tracking_number not in [nil, ""] ->
        "Tracking: #{order.tracking_number}\n#{order.tracking_url}\n\n"

      order.tracking_number not in [nil, ""] ->
        "Tracking: #{order.tracking_number}\n\n"

      true ->
        "Tracking details will follow once the carrier updates.\n\n"
    end
  end
end
