defmodule ToweropsWeb.AccountLive.ActivityTest do
  use ToweropsWeb.ConnCase, async: true

  import Phoenix.LiveViewTest

  alias Towerops.Admin

  setup :register_and_log_in_user_with_sudo

  describe "mount" do
    test "renders activity log page", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/account/activity")

      assert html =~ "Activity Log"
    end

    test "displays audit logs for current user", %{conn: conn, user: user} do
      {:ok, _log} =
        Admin.create_audit_log(%{
          action: "user_data_viewed",
          superuser_id: user.id,
          target_user_id: user.id
        })

      {:ok, _view, html} = live(conn, ~p"/account/activity")

      assert html =~ "User data viewed"
    end

    test "shows sudo_mode_granted from setup", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/account/activity")

      assert html =~ "Sudo mode granted"
    end
  end

  describe "action descriptions" do
    test "displays user_data_exported action", %{conn: conn, user: user} do
      {:ok, _} =
        Admin.create_audit_log(%{
          action: "user_data_exported",
          superuser_id: user.id,
          target_user_id: user.id
        })

      {:ok, _view, html} = live(conn, ~p"/account/activity")
      assert html =~ "exported your account data"
    end

    test "displays user_profile_updated action", %{conn: conn, user: user} do
      {:ok, _} =
        Admin.create_audit_log(%{
          action: "user_profile_updated",
          superuser_id: user.id,
          target_user_id: user.id
        })

      {:ok, _view, html} = live(conn, ~p"/account/activity")
      assert html =~ "updated your profile"
    end

    test "displays device_created action with metadata", %{conn: conn, user: user} do
      {:ok, _} =
        Admin.create_audit_log(%{
          action: "device_created",
          superuser_id: user.id,
          target_user_id: user.id,
          metadata: %{"device_name" => "Test Router"}
        })

      {:ok, _view, html} = live(conn, ~p"/account/activity")
      assert html =~ "Test Router"
    end

    test "displays device_deleted action", %{conn: conn, user: user} do
      {:ok, _} =
        Admin.create_audit_log(%{
          action: "device_deleted",
          superuser_id: user.id,
          target_user_id: user.id,
          metadata: %{"device_name" => "Old Router"}
        })

      {:ok, _view, html} = live(conn, ~p"/account/activity")
      assert html =~ "Old Router"
    end

    test "displays device_updated action", %{conn: conn, user: user} do
      {:ok, _} =
        Admin.create_audit_log(%{
          action: "device_updated",
          superuser_id: user.id,
          target_user_id: user.id,
          metadata: %{"device_id" => "abc-123"}
        })

      {:ok, _view, html} = live(conn, ~p"/account/activity")
      assert html =~ "abc-123"
    end

    test "displays failed_access_attempt action", %{conn: conn, user: user} do
      {:ok, _} =
        Admin.create_audit_log(%{
          action: "failed_access_attempt",
          superuser_id: user.id,
          target_user_id: user.id,
          metadata: %{"resource" => "/admin/secret"}
        })

      {:ok, _view, html} = live(conn, ~p"/account/activity")
      assert html =~ "/admin/secret"
    end

    test "displays privilege_escalation action", %{conn: conn, user: user} do
      {:ok, _} =
        Admin.create_audit_log(%{
          action: "privilege_escalation",
          superuser_id: user.id,
          target_user_id: user.id,
          metadata: %{"from_role" => "member", "to_role" => "admin"}
        })

      {:ok, _view, html} = live(conn, ~p"/account/activity")
      assert html =~ "member"
      assert html =~ "admin"
    end

    test "displays impersonate_start action", %{conn: conn, user: user} do
      {:ok, _} =
        Admin.create_audit_log(%{
          action: "impersonate_start",
          superuser_id: user.id,
          target_user_id: user.id
        })

      {:ok, _view, html} = live(conn, ~p"/account/activity")
      assert html =~ "impersonating"
    end

    test "displays impersonate_end action", %{conn: conn, user: user} do
      {:ok, _} =
        Admin.create_audit_log(%{
          action: "impersonate_end",
          superuser_id: user.id,
          target_user_id: user.id
        })

      {:ok, _view, html} = live(conn, ~p"/account/activity")
      assert html =~ "stopped impersonating"
    end

    test "displays org_data_accessed action", %{conn: conn, user: user} do
      {:ok, _} =
        Admin.create_audit_log(%{
          action: "org_data_accessed",
          superuser_id: user.id,
          target_user_id: user.id,
          metadata: %{"organization_id" => "org-123", "action" => "exported"}
        })

      {:ok, _view, html} = live(conn, ~p"/account/activity")
      assert html =~ "org-123"
    end

    test "displays fallback for action without handler", %{conn: conn, user: user} do
      {:ok, _} =
        Admin.create_audit_log(%{
          action: "monitoring_data_queried",
          superuser_id: user.id,
          target_user_id: user.id
        })

      {:ok, _view, html} = live(conn, ~p"/account/activity")
      assert html =~ "Monitoring data queried"
    end
  end
end
