defmodule ToweropsWeb.HelpLive.IndexTest do
  use ToweropsWeb.ConnCase, async: true

  import Phoenix.LiveViewTest

  describe "unauthenticated access" do
    test "mounts without authentication and shows help content", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/help")

      assert html =~ "Help"
      assert html =~ "About Towerops"
    end

    test "defaults to the about section", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/help")

      assert html =~ "About Towerops"
      assert html =~ "network monitoring"
    end

    test "navigates to getting-started section", %{conn: conn} do
      {:ok, view, _html} = live(conn, ~p"/help")

      html =
        view
        |> element("nav a[href='/help?section=getting-started']")
        |> render_click()

      assert html =~ "Getting Started"
      assert html =~ "Quick Start Guide"
    end

    test "navigates to sites section via params", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/help?section=sites")

      assert html =~ "Sites"
      assert html =~ "When to Enable Sites"
    end

    test "navigates to cloud-pollers section via params", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/help?section=cloud-pollers")

      assert html =~ "Cloud Pollers"
    end

    test "navigates to agents section via params", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/help?section=agents")

      assert html =~ "Remote Pollers"
    end

    test "navigates to graphs section via params", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/help?section=graphs")

      assert html =~ "Graphs"
      assert html =~ "Live Polling"
    end

    test "navigates to mikrotik section via params", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/help?section=mikrotik")

      assert html =~ "MikroTik"
    end

    test "shows section not found for unknown section", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/help?section=nonexistent")

      assert html =~ "Section Not Found"
    end
  end

  describe "authenticated access" do
    setup :register_and_log_in_user

    test "mounts with authentication and shows help content", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/help")

      assert html =~ "Help"
      assert html =~ "About Towerops"
    end

    test "navigates between sections via patch", %{conn: conn} do
      {:ok, view, _html} = live(conn, ~p"/help")

      # Navigate to getting-started section via sidebar nav
      html =
        view
        |> element("nav a[href='/help?section=getting-started']")
        |> render_click()

      assert html =~ "Getting Started"

      # Navigate to agents section via sidebar nav
      html =
        view
        |> element("nav a[href='/help?section=agents']")
        |> render_click()

      assert html =~ "Remote Pollers"
    end
  end
end
