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

  import Phoenix.LiveViewTest

  alias AprsmeWeb.StatusLive.ConnectionCardComponent

  defp assigns(overrides \\ %{}) do
    defaults = %{
      aprs_status: %{
        connected: true,
        server: "rotate.aprs.net",
        port: 14_580,
        login_id: "K5ABC",
        connected_at: ~U[2024-01-15 12:00:00Z],
        uptime_seconds: 3600,
        filter: "m/100",
        packet_stats: %{
          total_packets: 12_345,
          packets_per_second: 4,
          last_packet_at: DateTime.utc_now()
        },
        stored_packet_count: 99_999
      },
      health_score: 4
    }

    Map.merge(defaults, overrides)
  end

  describe "render/1" do
    test "renders Connected badge when aprs_status.connected is true" do
      html = render_component(&ConnectionCardComponent.render/1, assigns())
      assert html =~ "Connected"
      refute html =~ "Disconnected"
    end

    test "renders Disconnected badge when aprs_status.connected is false" do
      a = assigns()
      a = %{a | aprs_status: %{a.aprs_status | connected: false}}
      html = render_component(&ConnectionCardComponent.render/1, a)
      assert html =~ "Disconnected"
    end

    test "shows 'Not connected' when connected_at is nil" do
      a = assigns()
      a = %{a | aprs_status: %{a.aprs_status | connected_at: nil}}
      html = render_component(&ConnectionCardComponent.render/1, a)
      assert html =~ "Not connected"
    end

    test "shows 'None' for last_packet_at when nil" do
      a = assigns()
      stats = %{a.aprs_status.packet_stats | last_packet_at: nil}
      a = %{a | aprs_status: %{a.aprs_status | packet_stats: stats}}
      html = render_component(&ConnectionCardComponent.render/1, a)
      assert html =~ "None"
    end

    test "includes server and port" do
      html = render_component(&ConnectionCardComponent.render/1, assigns())
      assert html =~ "rotate.aprs.net"
      assert html =~ "14580"
    end

    test "includes login_id" do
      html = render_component(&ConnectionCardComponent.render/1, assigns())
      assert html =~ "K5ABC"
    end

    test "renders packets_per_minute as packets_per_second * 60" do
      html = render_component(&ConnectionCardComponent.render/1, assigns())
      # 4 * 60 = 240
      assert html =~ "240"
    end

    test "renders health score numerator" do
      html = render_component(&ConnectionCardComponent.render/1, assigns(%{health_score: 2}))
      assert html =~ "2/5"
    end
  end
end
