defmodule MicrowavepropWeb.LocationResolverTest do
  use ExUnit.Case, async: true

  alias MicrowavepropWeb.LocationResolver

  describe "resolve/1" do
    test "returns :empty for nil and blank input" do
      assert LocationResolver.resolve(nil) == :empty
      assert LocationResolver.resolve("") == :empty
      assert LocationResolver.resolve("   ") == :empty
    end

    test "parses comma-separated decimal coordinates" do
      assert {:ok, resolved} = LocationResolver.resolve("32.9, -96.8")
      assert resolved.kind == :coordinates
      assert resolved.lat == 32.9
      assert resolved.lon == -96.8
      assert resolved.label == "32.9, -96.8"
    end

    test "tolerates extra whitespace around coordinates" do
      assert {:ok, %{lat: 32.9, lon: -96.8}} = LocationResolver.resolve("  32.9 , -96.8  ")
    end

    test "parses Maidenhead grid squares (uppercase)" do
      assert {:ok, resolved} = LocationResolver.resolve("EM12kx")
      assert resolved.kind == :grid
      assert resolved.label == "EM12KX"
      assert is_float(resolved.lat)
      assert is_float(resolved.lon)
    end

    test "uppercases callsign-shaped grid squares for the label" do
      assert {:ok, %{label: "FN42"}} = LocationResolver.resolve("fn42")
    end
  end

  describe "coordinate_pair?/1" do
    test "true for two comma-separated floats" do
      assert LocationResolver.coordinate_pair?("32.9, -96.8")
      assert LocationResolver.coordinate_pair?("0,0")
      assert LocationResolver.coordinate_pair?("-89.9999, 179.9999")
    end

    test "false for malformed input" do
      refute LocationResolver.coordinate_pair?("EM12kx")
      refute LocationResolver.coordinate_pair?("32.9")
      refute LocationResolver.coordinate_pair?("a, b")
      refute LocationResolver.coordinate_pair?("")
    end
  end

  describe "maidenhead?/1" do
    test "true for valid grid squares" do
      assert LocationResolver.maidenhead?("EM12")
      assert LocationResolver.maidenhead?("EM12kx")
      assert LocationResolver.maidenhead?("em12kx99")
    end

    test "false for non-grid input" do
      refute LocationResolver.maidenhead?("32.9, -96.8")
      refute LocationResolver.maidenhead?("W5XYZ")
      refute LocationResolver.maidenhead?("EM")
      refute LocationResolver.maidenhead?("")
    end
  end
end
