defmodule MvWeb.MemberLive.FormErrorHandlingTest do
  @moduledoc """
  Tests for error handling in the member form, specifically flash message display.
  """
  use MvWeb.ConnCase, async: false

  import Phoenix.LiveViewTest

  require Ash.Query

  describe "error handling - flash messages" do
    @describetag :ui
    test "shows flash message when member creation fails with validation error", %{conn: conn} do
      system_actor = Mv.Helpers.SystemActor.get_system_actor()

      # Create a member with the same email to trigger uniqueness error
      {:ok, _existing_member} =
        Mv.Membership.create_member(
          %{
            first_name: "Existing",
            last_name: "Member",
            email: "duplicate@example.com"
          },
          actor: system_actor
        )

      conn = conn_with_oidc_user(conn)
      {:ok, view, _html} = live(conn, "/members/new")

      # Try to create member with duplicate email
      form_data = %{
        "member[first_name]" => "New",
        "member[last_name]" => "Member",
        "member[email]" => "duplicate@example.com"
      }

      html =
        view
        |> form("#member-form", form_data)
        |> render_submit()

      # Should show flash error message
      assert has_element?(view, "#flash-group")

      assert html =~ "error" or html =~ "Error" or html =~ "Fehler" or
               html =~ "failed" or html =~ "fehlgeschlagen" or
               html =~ "Validation failed" or html =~ "Validierung fehlgeschlagen"
    end

    test "shows flash message when member creation fails with missing required fields", %{
      conn: conn
    } do
      conn = conn_with_oidc_user(conn)
      {:ok, view, _html} = live(conn, "/members/new")

      # Submit form with missing required fields (e.g., email)
      form_data = %{
        "member[first_name]" => "Test",
        "member[last_name]" => "User"
        # email is missing
      }

      html =
        view
        |> form("#member-form", form_data)
        |> render_submit()

      # Should show flash error message
      assert has_element?(view, "#flash-group")

      assert html =~ "error" or html =~ "Error" or html =~ "Fehler" or
               html =~ "failed" or html =~ "fehlgeschlagen" or
               html =~ "Validation failed" or html =~ "Validierung fehlgeschlagen" or
               html =~ "Please correct" or html =~ "Bitte korrigieren"
    end

    test "shows flash message when member update fails", %{conn: conn} do
      system_actor = Mv.Helpers.SystemActor.get_system_actor()

      # Create a member to edit
      {:ok, member} =
        Mv.Membership.create_member(
          %{
            first_name: "Original",
            last_name: "Member",
            email: "original@example.com"
          },
          actor: system_actor
        )

      # Create another member with different email
      {:ok, _other_member} =
        Mv.Membership.create_member(
          %{first_name: "Other", last_name: "Member", email: "other@example.com"},
          actor: system_actor
        )

      conn = conn_with_oidc_user(conn)
      {:ok, view, _html} = live(conn, "/members/#{member.id}/edit")

      # Try to update with duplicate email
      form_data = %{
        "member[first_name]" => "Updated",
        "member[last_name]" => "Member",
        "member[email]" => "other@example.com"
      }

      html =
        view
        |> form("#member-form", form_data)
        |> render_submit()

      # Should show flash error message
      assert has_element?(view, "#flash-group")

      assert html =~ "error" or html =~ "Error" or html =~ "Fehler" or
               html =~ "failed" or html =~ "fehlgeschlagen" or
               html =~ "Validation failed" or html =~ "Validierung fehlgeschlagen"
    end

    test "form still displays field-level validation errors when flash message is shown", %{
      conn: conn
    } do
      conn = conn_with_oidc_user(conn)
      {:ok, view, _html} = live(conn, "/members/new")

      # Submit form with invalid email format
      form_data = %{
        "member[first_name]" => "Test",
        "member[last_name]" => "User",
        "member[email]" => "invalid-email-format"
      }

      html =
        view
        |> form("#member-form", form_data)
        |> render_submit()

      # Should show both flash message and field-level error
      assert has_element?(view, "#flash-group")
      # Field-level errors should also be visible in the form
      assert html =~ "email" or html =~ "Email"
    end
  end
end
