defmodule AprsmeWeb.StatusLive.IndexHelpersTest do
  use ExUnit.Case, async: true

  alias AprsmeWeb.StatusLive.Index

  describe "format_uptime/1" do
    test "returns 'Not connected' for zero and negative" do
      assert Index.format_uptime(0) =~ "Not connected"
      assert Index.format_uptime(-10) =~ "Not connected"
    end

    test "seconds-only for values under a minute" do
      assert Index.format_uptime(5) == "5s"
    end

    test "minutes and seconds for values under an hour" do
      assert Index.format_uptime(125) == "2m 5s"
    end

    test "hours/minutes/seconds for values under a day" do
      assert Index.format_uptime(3 * 3600 + 4 * 60 + 5) == "3h 4m 5s"
    end

    test "days/hours/minutes/seconds for longer values" do
      assert Index.format_uptime(2 * 86_400 + 1 * 3600 + 30 * 60 + 15) == "2d 1h 30m 15s"
    end
  end

  describe "format_time_ago/1" do
    test "returns 'Never' for nil" do
      assert Index.format_time_ago(nil) =~ "Never"
    end

    test "formats seconds" do
      dt = DateTime.add(DateTime.utc_now(), -10, :second)
      result = Index.format_time_ago(dt)
      assert result =~ "seconds ago"
    end

    test "formats minutes" do
      dt = DateTime.add(DateTime.utc_now(), -120, :second)
      result = Index.format_time_ago(dt)
      assert result =~ "minutes ago"
    end

    test "formats hours" do
      dt = DateTime.add(DateTime.utc_now(), -3 * 3600, :second)
      result = Index.format_time_ago(dt)
      assert result =~ "hours ago"
    end

    test "formats days" do
      dt = DateTime.add(DateTime.utc_now(), -3 * 86_400, :second)
      result = Index.format_time_ago(dt)
      assert result =~ "days ago"
    end
  end

  describe "get_health_description/2" do
    test "returns connection-issues text for score 1 with disconnect" do
      assert Index.get_health_description(1, false) =~ "Disconnected"
    end

    test "returns stability text for scores 2-5 when connected" do
      assert Index.get_health_description(2, true) =~ "Recently"
      assert Index.get_health_description(3, true) =~ "Good"
      assert Index.get_health_description(4, true) =~ "Very good"
      assert Index.get_health_description(5, true) =~ "Excellent"
    end

    test "returns unknown text for mismatched (score, connected) combos" do
      assert Index.get_health_description(5, false) =~ "Unknown"
      assert Index.get_health_description(99, true) =~ "Unknown"
    end
  end

  describe "format_number/1" do
    test "adds comma thousand separators to integers" do
      assert Index.format_number(1_000) == "1,000"
      assert Index.format_number(1_234_567) == "1,234,567"
    end

    test "leaves small integers untouched" do
      assert Index.format_number(42) == "42"
      assert Index.format_number(999) == "999"
    end

    test "handles zero and negative integers" do
      assert Index.format_number(0) == "0"
      # Negative number formatting: preserve the minus sign in front.
      assert Index.format_number(-1_234) =~ "1,234"
    end

    test "converts non-integer input to string" do
      assert Index.format_number(1.5) == "1.5"
      assert Index.format_number(nil) == ""
      assert Index.format_number(:atom) == "atom"
    end
  end
end
