defmodule ToweropsWeb.MobileQRLiveTest do
  use ToweropsWeb.ConnCase, async: true

  import Phoenix.LiveViewTest

  describe "authenticated access" do
    setup :register_and_log_in_user

    test "mounts and shows QR code page", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/mobile/qr-login")

      assert html =~ "Mobile App Login"
      assert html =~ "Scan"
      assert html =~ "QR"
    end

    test "displays QR code image", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/mobile/qr-login")

      assert html =~ "qrcode"
      assert html =~ "<img"
    end

    test "shows expiration notice", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/mobile/qr-login")

      assert html =~ "expires"
    end

    test "shows polling indicator", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/mobile/qr-login")

      assert html =~ "Checking"
    end

    test "shows link back to organizations", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/mobile/qr-login")

      assert html =~ ~p"/orgs"
    end
  end

  describe "unauthenticated access" do
    test "requires authentication", %{conn: conn} do
      {:error, redirect} = live(conn, ~p"/mobile/qr-login")

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