defmodule Microwaveprop.Rover.LinkMarginTest do
  use ExUnit.Case, async: false

  alias Microwaveprop.Rover.LinkMargin

  describe "score_to_db/1" do
    test "0 maps to -25.0 dB" do
      assert LinkMargin.score_to_db(0) == -25.0
    end

    test "50 maps to 0.0 dB" do
      assert LinkMargin.score_to_db(50) == 0.0
    end

    test "100 maps to 25.0 dB" do
      assert LinkMargin.score_to_db(100) == 25.0
    end

    test "nil maps to nil" do
      assert LinkMargin.score_to_db(nil) == nil
    end

    test "negative score maps below -25 dB" do
      assert LinkMargin.score_to_db(-10) == -30.0
    end

    test "score above 100 maps above 25 dB" do
      assert LinkMargin.score_to_db(120) == 35.0
    end
  end

  describe "link_margin_from_score/2" do
    test "score 80 in :ssb mode = 15.0 dB" do
      assert LinkMargin.link_margin_from_score(80, :ssb) == 15.0
    end

    test "score 50 in :cw mode = 18.0 dB (0 - (-18))" do
      assert LinkMargin.link_margin_from_score(50, :cw) == 18.0
    end

    test "nil score returns nil" do
      assert LinkMargin.link_margin_from_score(nil, :ssb) == nil
    end

    test "score 0 in :cw mode = -7.0 dB" do
      assert LinkMargin.link_margin_from_score(0, :cw) == -7.0
    end

    test "score 100 in :cw mode = 43.0 dB" do
      assert LinkMargin.link_margin_from_score(100, :cw) == 43.0
    end

    test "score 30 in :ssb mode gives negative margin" do
      margin = LinkMargin.link_margin_from_score(30, :ssb)
      assert margin < 0
    end

    test "score 50 in :q65_60a mode = 24.0 dB" do
      assert LinkMargin.link_margin_from_score(50, :q65_60a) == 24.0
    end
  end

  describe "link_margin_db/5" do
    alias Microwaveprop.Propagation.ScoresFile

    setup do
      prev = Application.get_env(:microwaveprop, :propagation_scores_dir)
      dir = Path.join(System.tmp_dir!(), "link_margin_test_#{System.unique_integer([:positive])}")

      Application.put_env(:microwaveprop, :propagation_scores_dir, dir)

      on_exit(fn ->
        File.rm_rf(dir)

        if prev do
          Application.put_env(:microwaveprop, :propagation_scores_dir, prev)
        else
          Application.delete_env(:microwaveprop, :propagation_scores_dir)
        end
      end)

      :ok
    end

    test "returns nil when no score data exists" do
      assert LinkMargin.link_margin_db(10_000, ~U[2026-05-01 12:00:00Z], {33.0, -97.0}, {33.1, -96.5}, :ssb) ==
               nil
    end

    test "returns margin when score exists for the cell" do
      valid_time = ~U[2026-05-01 12:00:00Z]
      ScoresFile.write!(10_000, valid_time, [%{lat: 33.0, lon: -97.0, score: 80}])

      margin =
        LinkMargin.link_margin_db(10_000, valid_time, {33.0, -97.0}, {33.1, -96.5}, :ssb)

      # score_to_db(80) = 15.0, ssb threshold = 0.0, so margin = 15.0
      assert_in_delta margin, 15.0, 0.1
    end

    test "returns nil for a cell not in the score file" do
      valid_time = ~U[2026-05-01 12:00:00Z]
      ScoresFile.write!(10_000, valid_time, [%{lat: 33.0, lon: -97.0, score: 80}])

      assert LinkMargin.link_margin_db(10_000, valid_time, {35.0, -95.0}, {33.1, -96.5}, :ssb) == nil
    end
  end
end
