defmodule ToweropsWeb.DeviceLive.ShowTest do
  use ToweropsWeb.ConnCase

  import Phoenix.LiveViewTest

  alias Towerops.Monitoring
  alias Towerops.Organizations
  alias Towerops.Sites

  setup :register_and_log_in_user

  setup %{user: user} do
    {:ok, organization} = Organizations.create_organization(%{name: "Test Org"}, user.id)

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

    {:ok, device} =
      Towerops.Devices.create_device(%{
        name: "Test Router",
        ip_address: "192.168.1.1",
        site_id: site.id
      })

    %{organization: organization, site: site, device: device}
  end

  describe "Show" do
    test "displays device information", %{conn: conn, device: device, organization: org} do
      {:ok, _view, html} = live(conn, ~p"/orgs/#{org.slug}/devices/#{device.id}")

      assert html =~ device.name
      assert html =~ device.ip_address
    end

    test "displays overview tab by default", %{conn: conn, device: device, organization: org} do
      {:ok, _view, html} = live(conn, ~p"/orgs/#{org.slug}/devices/#{device.id}")

      assert html =~ "Overview"
    end

    test "displays metrics when device has checks", %{
      conn: conn,
      device: device,
      organization: org
    } do
      # Create some checks
      Monitoring.create_check(%{
        device_id: device.id,
        status: :success,
        response_time_ms: 10,
        checked_at: DateTime.utc_now()
      })

      Monitoring.create_check(%{
        device_id: device.id,
        status: :success,
        response_time_ms: 15,
        checked_at: DateTime.utc_now()
      })

      Monitoring.create_check(%{
        device_id: device.id,
        status: :failure,
        response_time_ms: nil,
        checked_at: DateTime.utc_now()
      })

      {:ok, _view, html} = live(conn, ~p"/orgs/#{org.slug}/devices/#{device.id}")

      assert html =~ "Device Information"
    end

    test "displays empty state when no checks exist", %{
      conn: conn,
      device: device,
      organization: org
    } do
      {:ok, _view, html} = live(conn, ~p"/orgs/#{org.slug}/devices/#{device.id}")

      assert html =~ device.name
    end

    test "refreshes data periodically", %{conn: conn, device: device, organization: org} do
      {:ok, view, _html} = live(conn, ~p"/orgs/#{org.slug}/devices/#{device.id}")

      # Trigger a status change
      Monitoring.create_check(%{
        device_id: device.id,
        status: :failure,
        response_time_ms: nil,
        checked_at: DateTime.utc_now()
      })

      # Send refresh message
      send(view.pid, :refresh_data)

      # Give it time to process
      :timer.sleep(100)

      # View should still be alive
      assert render(view)
    end

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

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

    test "handles equipment_status_changed event", %{
      conn: conn,
      device: device,
      organization: org
    } do
      {:ok, view, _html} = live(conn, ~p"/orgs/#{org.slug}/devices/#{device.id}")

      # Simulate status change event
      send(view.pid, {:device_status_changed, device.id, :up, 25})

      # Give it time to process
      :timer.sleep(100)

      # View should still be alive and render
      assert render(view)
    end

    test "handles discovery_completed event", %{
      conn: conn,
      device: device,
      organization: org
    } do
      {:ok, view, _html} = live(conn, ~p"/orgs/#{org.slug}/devices/#{device.id}")

      # Simulate discovery completed event
      send(view.pid, {:discovery_completed, device.id})

      # Give it time to process
      :timer.sleep(100)

      html = render(view)
      assert html =~ "Discovery completed"
    end

    test "switches to different tabs", %{conn: conn, device: device, organization: org} do
      {:ok, _view, html} = live(conn, ~p"/orgs/#{org.slug}/devices/#{device.id}?tab=events")

      assert html =~ device.name
    end
  end
end
