defmodule MicrowavepropWeb.UserProfileLiveTest do
  use MicrowavepropWeb.ConnCase, async: true

  import Microwaveprop.AccountsFixtures
  import Microwaveprop.BeaconsFixtures
  import Phoenix.LiveViewTest

  alias Microwaveprop.Beacons
  alias Microwaveprop.Radio.Contact
  alias Microwaveprop.Repo

  defp contact_for(user, attrs) do
    defaults = %{
      station1: "W5XD",
      station2: "K5TR",
      qso_timestamp: ~U[2026-03-28 18:00:00Z],
      mode: "CW",
      band: Decimal.new("10000"),
      grid1: "EM12",
      grid2: "EM00",
      pos1: %{"lat" => 32.9, "lon" => -97.0},
      pos2: %{"lat" => 30.3, "lon" => -97.7},
      distance_km: Decimal.new("295"),
      user_id: user.id
    }

    {:ok, contact} =
      %Contact{}
      |> Contact.changeset(Map.merge(defaults, attrs))
      |> Repo.insert()

    contact
  end

  describe "mount" do
    test "renders a user's contributions", %{conn: conn} do
      user = user_fixture(%{callsign: "W5ISP"})
      _contact = contact_for(user, %{station1: "W5OWN"})
      _beacon = beacon_fixture(user, callsign: "W5ISP")

      {:ok, _lv, html} = live(conn, ~p"/u/#{user.callsign}")

      assert html =~ "W5ISP"
      assert html =~ "Contacts submitted"
      assert html =~ "Beacons submitted"
      assert html =~ "W5OWN"
    end

    test "callsign lookup is case-insensitive", %{conn: conn} do
      _user = user_fixture(%{callsign: "W5ISP"})

      {:ok, _lv, html} = live(conn, ~p"/u/w5isp")

      assert html =~ "W5ISP"
    end

    test "does not show contacts belonging to another user", %{conn: conn} do
      user = user_fixture(%{callsign: "W5ISP"})
      other = user_fixture(%{callsign: "W5OTH"})
      _owned = contact_for(user, %{station1: "W5MINE"})
      _theirs = contact_for(other, %{station1: "W5NOTMINE"})

      {:ok, _lv, html} = live(conn, ~p"/u/#{user.callsign}")

      assert html =~ "W5MINE"
      refute html =~ "W5NOTMINE"
    end

    test "shows empty-state messages when the user has no submissions", %{conn: conn} do
      user = user_fixture(%{callsign: "W5EMP"})

      {:ok, _lv, html} = live(conn, ~p"/u/#{user.callsign}")

      assert html =~ "No contacts submitted yet"
      assert html =~ "No beacons submitted yet"
    end

    test "redirects to the home page for an unknown callsign", %{conn: conn} do
      assert {:error, {:live_redirect, %{to: "/"}}} = live(conn, ~p"/u/NO1SUCH")
    end

    test "lists both approved and pending beacons so owners see their drafts", %{conn: conn} do
      user = user_fixture(%{callsign: "W5ISP"})
      {:ok, approved} = Beacons.create_beacon(user, valid_beacon_attrs())
      {:ok, _approved} = Beacons.approve_beacon(approved)
      _pending = beacon_fixture(user, callsign: "W5PND")

      {:ok, _lv, html} = live(conn, ~p"/u/#{user.callsign}")

      assert html =~ "W5PND"
      assert html =~ "Pending"
      assert html =~ "Approved"
    end
  end
end
