defmodule AprsmeWeb.WeatherUnitsTest do
  use ExUnit.Case, async: true

  alias AprsmeWeb.WeatherUnits

  describe "unit_system/1" do
    test "returns :imperial for English locale" do
      assert WeatherUnits.unit_system("en") == :imperial
    end

    test "returns :metric for common metric locales" do
      assert WeatherUnits.unit_system("es") == :metric
      assert WeatherUnits.unit_system("de") == :metric
      assert WeatherUnits.unit_system("fr") == :metric
    end

    test "defaults to :metric for other locale strings" do
      assert WeatherUnits.unit_system("ja") == :metric
      assert WeatherUnits.unit_system("zh") == :metric
    end

    test "defaults to :imperial for nil and non-binary" do
      assert WeatherUnits.unit_system(nil) == :imperial
      assert WeatherUnits.unit_system(123) == :imperial
      assert WeatherUnits.unit_system(:atom) == :imperial
    end
  end

  describe "format_temperature/2" do
    test "returns Fahrenheit unchanged for English locale" do
      assert WeatherUnits.format_temperature(72, "en") == {72, "°F"}
    end

    test "converts Fahrenheit to Celsius for metric locale" do
      {value, unit} = WeatherUnits.format_temperature(32, "de")
      assert_in_delta value, 0.0, 0.0001
      assert unit == "°C"
    end

    test "returns value and °F for non-numeric input" do
      assert WeatherUnits.format_temperature(nil, "en") == {nil, "°F"}
      assert WeatherUnits.format_temperature("N/A", "de") == {"N/A", "°F"}
    end
  end

  describe "format_wind_speed/2" do
    test "returns mph for English locale" do
      assert WeatherUnits.format_wind_speed(10, "en") == {10, "mph"}
    end

    test "converts mph to km/h for metric locale" do
      {value, unit} = WeatherUnits.format_wind_speed(10, "fr")
      assert_in_delta value, 16.0934, 0.0001
      assert unit == "km/h"
    end

    test "returns value and mph for non-numeric input" do
      assert WeatherUnits.format_wind_speed(nil, "de") == {nil, "mph"}
    end
  end

  describe "format_rain/2" do
    test "returns inches for English locale" do
      assert WeatherUnits.format_rain(1.5, "en") == {1.5, "in"}
    end

    test "converts inches to mm for metric locale" do
      {value, unit} = WeatherUnits.format_rain(1, "es")
      assert_in_delta value, 25.4, 0.0001
      assert unit == "mm"
    end

    test "returns value and in for non-numeric input" do
      assert WeatherUnits.format_rain(nil, "en") == {nil, "in"}
    end
  end

  describe "format_pressure/2" do
    test "returns hPa for any locale" do
      assert WeatherUnits.format_pressure(1013, "en") == {1013, "hPa"}
      assert WeatherUnits.format_pressure(1013, "de") == {1013, "hPa"}
    end

    test "returns hPa even for non-numeric input" do
      assert WeatherUnits.format_pressure(nil, "en") == {nil, "hPa"}
    end
  end

  describe "unit_labels/1" do
    test "returns imperial labels for English" do
      assert WeatherUnits.unit_labels("en") == %{
               temperature: "°F",
               wind_speed: "mph",
               rain: "in",
               pressure: "hPa"
             }
    end

    test "returns metric labels for metric locale" do
      assert WeatherUnits.unit_labels("de") == %{
               temperature: "°C",
               wind_speed: "km/h",
               rain: "mm",
               pressure: "hPa"
             }
    end
  end
end
