defmodule Microwaveprop.Format do
  @moduledoc """
  Shared human-facing formatters. Miles are the primary distance unit
  across the site; km appears parenthetically for readers working in
  metric.
  """

  @doc """
  Format a distance in km as "X mi (Y km)". Under 10 miles the output
  carries one decimal so near-LOS paths don't round to zero; longer
  distances round to whole numbers.
  """
  @spec distance_km(number() | Decimal.t() | nil) :: String.t()
  def distance_km(nil), do: "—"

  def distance_km(%Decimal{} = d), do: d |> Decimal.to_float() |> distance_km()

  def distance_km(km) when is_number(km) do
    mi = km / 1.609344

    {mi_str, km_str} =
      if mi < 10.0,
        do: {one_decimal(mi), one_decimal(km)},
        else: {Integer.to_string(round(mi)), Integer.to_string(round(km))}

    "#{mi_str} mi (#{km_str} km)"
  end

  defp one_decimal(n), do: :erlang.float_to_binary(n * 1.0, decimals: 1)
end
