defmodule ToweropsWeb.DashboardLiveTest do
  use ToweropsWeb.ConnCase

  import Phoenix.LiveViewTest

  setup :register_and_log_in_user

  setup %{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
      })

    %{organization: organization, site: site}
  end

  describe "Dashboard" do
    test "displays organization name and stats", %{conn: conn, organization: organization} do
      {:ok, _view, html} = live(conn, ~p"/orgs/#{organization.slug}")

      assert html =~ organization.name
      assert html =~ "Sites"
      assert html =~ "Device"
      assert html =~ "Active Alerts"
    end

    test "displays correct site count", %{conn: conn, organization: organization} do
      # Create additional sites
      {:ok, _site2} =
        Towerops.Sites.create_site(%{
          name: "Site 2",
          organization_id: organization.id
        })

      {:ok, _view, html} = live(conn, ~p"/orgs/#{organization.slug}")
      assert html =~ "Sites"
    end

    test "displays correct device counts", %{conn: conn, organization: organization, site: site} do
      # Create devices with different statuses
      {:ok, device_up} =
        Towerops.Devices.create_device(%{
          name: "Router 1",
          ip_address: "192.168.1.1",
          site_id: site.id,
          monitoring_enabled: true
        })

      Towerops.Devices.update_device_status(device_up, :up)

      {:ok, device_down} =
        Towerops.Devices.create_device(%{
          name: "Router 2",
          ip_address: "192.168.1.2",
          site_id: site.id,
          monitoring_enabled: true
        })

      Towerops.Devices.update_device_status(device_down, :down)

      {:ok, _view, html} = live(conn, ~p"/orgs/#{organization.slug}")
      assert html =~ "Device"
      assert html =~ "Up"
      assert html =~ "Down"
    end

    test "displays active alerts count", %{conn: conn, organization: organization, site: site} do
      {:ok, device} =
        Towerops.Devices.create_device(%{
          name: "Router 1",
          ip_address: "192.168.1.1",
          site_id: site.id
        })

      # Create an active 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"/orgs/#{organization.slug}")
      assert html =~ "Active Alerts"
    end

    test "updates in real-time when new alert is triggered", %{
      conn: conn,
      organization: organization,
      site: site
    } do
      {:ok, device} =
        Towerops.Devices.create_device(%{
          name: "Router 1",
          ip_address: "192.168.1.1",
          site_id: site.id
        })

      {:ok, view, _html} = live(conn, ~p"/orgs/#{organization.slug}")

      # Trigger a new alert via PubSub
      Phoenix.PubSub.broadcast(
        Towerops.PubSub,
        "alerts:new",
        {:new_alert, device.id, :device_down}
      )

      # View should update
      assert render(view)
    end

    test "stat cards are clickable links", %{conn: conn, organization: organization} do
      {:ok, _view, html} = live(conn, ~p"/orgs/#{organization.slug}")

      # Check for navigation links
      assert html =~ ~p"/orgs/#{organization.slug}/sites"
      assert html =~ ~p"/orgs/#{organization.slug}/devices"
      assert html =~ ~p"/orgs/#{organization.slug}/alerts"
    end

    test "requires authentication", %{organization: organization} do
      conn = build_conn()
      {:error, redirect} = live(conn, ~p"/orgs/#{organization.slug}")

      assert {:redirect, %{to: path}} = redirect
      assert path == ~p"/users/log-in"
    end
  end
end
