defmodule Microwaveprop.Radio.CallsignClient do
  @moduledoc """
  Public facade for callsign → location resolution. Delegates to
  `Microwaveprop.Radio.CallsignLocation`, which combines a cached
  QRZ.com lookup with a Google Maps fallback geocode, and reshapes
  the result so callers get back `{:ok, %{callsign, gridsquare, lat,
  lon}}`.
  """
  alias Microwaveprop.CallsignLocation

  @type location :: %{
          callsign: String.t(),
          gridsquare: String.t() | nil,
          lat: number(),
          lon: number()
        }

  @spec locate(String.t()) :: {:ok, location()} | {:error, String.t()}
  def locate(callsign) do
    case CallsignLocation.lookup(callsign) do
      {:ok, result} ->
        {:ok,
         %{
           callsign: result.callsign,
           gridsquare: result.gridsquare,
           lat: result.latitude,
           lon: result.longitude
         }}

      {:error, _} = error ->
        error
    end
  end
end
