defmodule AprsmeWeb.MapLive.UrlParamsTest do
  use ExUnit.Case, async: true

  alias AprsmeWeb.MapLive.UrlParams

  describe "parse_map_params/1" do
    test "parses a full set of params" do
      {center, zoom} = UrlParams.parse_map_params(%{"lat" => "40.0", "lng" => "-95.0", "z" => "7"})
      assert center == %{lat: 40.0, lng: -95.0}
      assert zoom == 7
    end

    test "falls back to defaults for missing params" do
      {center, zoom} = UrlParams.parse_map_params(%{})
      assert center == UrlParams.default_center()
      assert zoom == UrlParams.default_zoom()
    end

    test "falls back to defaults for garbage values" do
      {center, zoom} = UrlParams.parse_map_params(%{"lat" => "foo", "lng" => "bar", "z" => "baz"})
      assert center == UrlParams.default_center()
      assert zoom == UrlParams.default_zoom()
    end

    test "rejects out-of-range values" do
      {center, _} = UrlParams.parse_map_params(%{"lat" => "200.0", "lng" => "400.0"})
      assert center == UrlParams.default_center()
    end
  end

  describe "parse_latitude/1 / parse_longitude/1 / parse_zoom/1" do
    test "individual parsers accept valid values" do
      assert UrlParams.parse_latitude("30.5") == 30.5
      assert UrlParams.parse_longitude("-120.0") == -120.0
      assert UrlParams.parse_zoom("10") == 10
    end

    test "individual parsers return defaults for nil" do
      assert UrlParams.parse_latitude(nil) == UrlParams.default_center().lat
      assert UrlParams.parse_longitude(nil) == UrlParams.default_center().lng
      assert UrlParams.parse_zoom(nil) == UrlParams.default_zoom()
    end

    test "individual parsers clamp invalid strings to defaults" do
      assert UrlParams.parse_latitude("garbage") == UrlParams.default_center().lat
      assert UrlParams.parse_zoom("garbage") == UrlParams.default_zoom()
    end
  end

  describe "has_explicit_url_params?/1" do
    test "returns true when any of lat, lng, or z is present" do
      assert UrlParams.has_explicit_url_params?(%{"lat" => "1"})
      assert UrlParams.has_explicit_url_params?(%{"lng" => "1"})
      assert UrlParams.has_explicit_url_params?(%{"z" => "5"})
    end

    test "returns false when none of lat, lng, z is present" do
      refute UrlParams.has_explicit_url_params?(%{})
      refute UrlParams.has_explicit_url_params?(%{"other" => "1"})
    end

    test "returns false when keys are present but empty-string (treated truthy)" do
      # Empty string is truthy in Elixir, so this actually returns true.
      assert UrlParams.has_explicit_url_params?(%{"lat" => ""})
    end
  end

  describe "defaults" do
    test "default_center returns USA-centered coordinates" do
      %{lat: lat, lng: lng} = UrlParams.default_center()
      assert lat >= 25 and lat <= 50
      assert lng >= -125 and lng <= -66
    end

    test "default_zoom is a valid zoom" do
      assert UrlParams.default_zoom() == 5
    end
  end

  describe "delegated helpers still work" do
    test "parse_float_in_range delegates to ParamUtils" do
      assert UrlParams.parse_float_in_range("30.5", 0.0, -90.0, 90.0) == 30.5
    end

    test "sanitize_numeric_string delegates to ParamUtils" do
      assert UrlParams.sanitize_numeric_string("1<x>2") == "12"
    end

    test "valid_coordinates? delegates to CoordinateUtils" do
      assert UrlParams.valid_coordinates?(30.5, -95.0)
      refute UrlParams.valid_coordinates?(200, 0)
    end

    test "parse_int_in_range delegates to ParamUtils" do
      assert UrlParams.parse_int_in_range("8", 5, 1, 20) == 8
      # Out-of-range falls back to default.
      assert UrlParams.parse_int_in_range("999", 5, 1, 20) == 5
    end

    test "limit_string_length delegates to ParamUtils" do
      assert UrlParams.limit_string_length("hello", 3) == "hel"
    end

    test "finite? delegates to ParamUtils" do
      assert UrlParams.finite?(1.5)
    end
  end
end
