defmodule Microwaveprop.Rover.ProminenceTest do
  use ExUnit.Case, async: true

  alias Microwaveprop.Rover.Prominence

  describe "prominence_map/2" do
    test "returns map keyed by {lat, lon}" do
      cells = [%{lat: 33.0, lon: -97.0}, %{lat: 33.1, lon: -97.1}]
      elev_lookup = fn points ->
        Map.new(points, fn p -> {p, 200} end)
      end

      result = Prominence.prominence_map(cells, elev_lookup: elev_lookup)

      assert is_map(result)
      assert Map.has_key?(result, {33.0, -97.0})
      assert Map.has_key?(result, {33.1, -97.1})
    end

    test "cell sitting above neighbours has positive prominence" do
      elev_lookup = fn points ->
        Map.new(points, fn
          {33.0, -97.0} -> {{33.0, -97.0}, 300}
          p -> {p, 100}
        end)
      end

      result = Prominence.prominence_map([%{lat: 33.0, lon: -97.0}], elev_lookup: elev_lookup)
      assert result[{33.0, -97.0}] > 0
    end

    test "cell at same elevation as neighbours has zero prominence" do
      elev_lookup = fn points ->
        Map.new(points, fn p -> {p, 150} end)
      end

      result = Prominence.prominence_map([%{lat: 33.0, lon: -97.0}], elev_lookup: elev_lookup)
      assert result[{33.0, -97.0}] == 0
    end

    test "neighbours with nil elevations are excluded from ring average" do
      elev_lookup = fn points ->
        Map.new(points, fn
          {33.0, -97.0} -> {{33.0, -97.0}, 200}
          p -> {p, nil}
        end)
      end

      result = Prominence.prominence_map([%{lat: 33.0, lon: -97.0}], elev_lookup: elev_lookup)
      assert result[{33.0, -97.0}] == nil
    end

    test "single cell returns nil prominence (no neighbours to compare)" do
      elev_lookup = fn _points -> %{{45.0, -120.0} => 500} end
      result = Prominence.prominence_map([%{lat: 45.0, lon: -120.0}], elev_lookup: elev_lookup)
      assert result[{45.0, -120.0}] == nil
    end

    test "empty cells list returns empty map" do
      assert Prominence.prominence_map([]) == %{}
    end
  end
end
