defmodule ExNVRWeb.UserLoginLiveTest do
  @moduledoc false

  use ExNVRWeb.ConnCase

  import Phoenix.LiveViewTest
  import ExNVR.AccountsFixtures

  describe "Log in page" do
    test "renders log in page", %{conn: conn} do
      {:ok, _lv, html} = live(conn, ~p"/users/login")

      assert html =~ "Sign in"
      assert html =~ "Forgot your password?"
    end

    test "redirects if already logged in", %{conn: conn} do
      result =
        conn
        |> log_in_user(user_fixture())
        |> live(~p"/users/login")
        |> follow_redirect(conn, "/dashboard")

      assert {:ok, _conn} = result
    end
  end

  describe "user login" do
    test "redirects if user login with valid credentials", %{conn: conn} do
      password = "123456789Abcd"
      user = user_fixture(%{password: password})

      {:ok, lv, _html} = live(conn, ~p"/users/login")

      form =
        form(lv, "#login_form", user: %{email: user.email, password: password, remember_me: true})

      conn = submit_form(form, conn)

      assert redirected_to(conn) == ~p"/dashboard"
    end

    test "redirects to login page with a flash error if there are no valid credentials", %{
      conn: conn
    } do
      {:ok, lv, _html} = live(conn, ~p"/users/login")

      form =
        form(lv, "#login_form",
          user: %{email: "test@email.com", password: "123456", remember_me: true}
        )

      conn = submit_form(form, conn)

      assert Phoenix.Flash.get(conn.assigns.flash, :error) == "Invalid email or password"

      assert redirected_to(conn) == "/users/login"
    end
  end

  describe "login navigation" do
    test "redirects to forgot password page when the Forgot Password button is clicked", %{
      conn: conn
    } do
      {:ok, lv, _html} = live(conn, ~p"/users/login")

      {:ok, conn} =
        lv
        |> element(~s|a[href="/users/reset-password"]|)
        |> render_click()
        |> follow_redirect(conn, ~p"/users/reset-password")

      assert conn.resp_body =~ "Forgot your password?"
    end
  end

  describe "logout" do
    test "when user logged in", %{conn: conn} do
      conn = conn |> log_in_user(user_fixture())

      {:ok, lv, _html} = live(conn, ~p"/dashboard")

      assert {:error, {:redirect, %{to: "/users/logout"}}} =
               lv |> element("a", "Sign out") |> render_click()
    end

    test "when user not logged in", %{conn: conn} do
      {:ok, lv, _html} = live(conn, ~p"/users/login")

      refute lv |> element("a", "Sign out") |> has_element?()
    end
  end
end
