defmodule ToweropsWeb.AccountLive.MyDataTest do
  use ToweropsWeb.ConnCase

  import Phoenix.LiveViewTest

  setup :register_and_log_in_user_with_sudo

  describe "My Data page" do
    test "renders for organization owner without additional data", %{conn: conn} do
      # register_and_log_in_user creates a user with a Personal org (as owner)
      {:ok, _view, html} = live(conn, ~p"/users/my-data")

      assert html =~ "My Data"
      assert html =~ "Profile Information"
      assert html =~ "Organizations"
      assert html =~ "Devices"
      assert html =~ "Audit Log"
    end

    test "renders for organization owner with full data", %{conn: conn, user: user} do
      {:ok, organization} = Towerops.Organizations.create_organization(%{name: "Test Org"}, user.id)

      {:ok, site} =
        Towerops.Sites.create_site(%{
          name: "Test Site",
          organization_id: organization.id
        })

      {:ok, device} =
        Towerops.Devices.create_device(%{
          name: "Test Device",
          ip_address: "192.168.1.1",
          site_id: site.id,
          organization_id: organization.id,
          monitoring_enabled: true
        })

      # Create an alert
      {:ok, _alert} =
        Towerops.Alerts.create_alert(%{
          device_id: device.id,
          alert_type: :device_down,
          triggered_at: DateTime.utc_now(),
          message: "Device is down"
        })

      {:ok, _view, html} = live(conn, ~p"/users/my-data")

      assert html =~ "My Data"
      assert html =~ "Profile Information"
      assert html =~ "Organizations"
      assert html =~ "Devices"
      assert html =~ "Recent Alerts"
      assert html =~ "Audit Log"
      assert html =~ "Test Org"
      assert html =~ "Test Device"
      assert html =~ "192.168.1.1"
    end
  end
end
