defmodule MvWeb.MemberFieldLive.IndexComponentTest do
  @moduledoc """
  Tests for MemberFieldLive.IndexComponent.

  Tests cover:
  - Rendering all member fields from Mv.Constants.member_fields()
  - Displaying show_in_overview status as badge (Yes/No)
  - Displaying required status for required fields (first_name, last_name, email)
  - Current status is displayed based on settings.member_field_visibility
  - Default status is "Yes" (visible) when not configured in settings
  """
  use MvWeb.ConnCase, async: false

  import Phoenix.LiveViewTest

  alias Mv.Membership

  setup %{conn: conn} do
    user = create_test_user(%{email: "admin@example.com"})
    conn = conn_with_oidc_user(conn, user)
    {:ok, conn: conn, user: user}
  end

  describe "rendering" do
    test "renders all member fields from Constants", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/settings")

      # Check that all member fields are displayed
      member_fields = Mv.Constants.member_fields()

      for field <- member_fields do
        field_name = String.replace(Atom.to_string(field), "_", " ") |> String.capitalize()
        # Field name should appear in the table (either as label or in some form)
        assert html =~ field_name or html =~ Atom.to_string(field)
      end
    end

    test "displays show_in_overview status as badge", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/settings")

      # Should have "Show in overview" column header
      assert html =~ "Show in overview" or html =~ "Show in Overview"

      # Should have badge elements (Yes/No)
      assert html =~ "badge" or html =~ "Yes" or html =~ "No"
    end

    test "displays required status for required fields", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/settings")

      # Required fields: first_name, last_name, email
      # Should have "Required" column or indicator
      assert html =~ "Required" or html =~ "required"
    end

    test "shows default status as Yes when not configured", %{conn: conn} do
      # Ensure settings have no member_field_visibility configured
      {:ok, settings} = Membership.get_settings()

      {:ok, _updated} =
        Membership.update_settings(settings, %{member_field_visibility: %{}})

      {:ok, _view, html} = live(conn, ~p"/settings")

      # All fields should show as visible (Yes) by default
      # Check for "Yes" badge or similar indicator
      assert html =~ "Yes" or html =~ "badge-success"
    end

    test "shows configured visibility status from settings", %{conn: conn} do
      # Configure some fields as hidden
      {:ok, settings} = Membership.get_settings()
      visibility_config = %{"street" => false, "house_number" => false}

      {:ok, _updated} =
        Membership.update_member_field_visibility(settings, visibility_config)

      {:ok, _view, html} = live(conn, ~p"/settings")

      # Street and house_number should show as hidden (No)
      # Other fields should show as visible (Yes)
      assert html =~ "street" or html =~ "Street"
      assert html =~ "house_number" or html =~ "House number"
    end
  end

  describe "required fields" do
    test "marks first_name as required", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/settings")

      # first_name should be marked as required
      assert html =~ "first_name" or html =~ "First name"
      # Should have required indicator
      assert html =~ "required" or html =~ "Required"
    end

    test "marks last_name as required", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/settings")

      # last_name should be marked as required
      assert html =~ "last_name" or html =~ "Last name"
      # Should have required indicator
      assert html =~ "required" or html =~ "Required"
    end

    test "marks email as required", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/settings")

      # email should be marked as required
      assert html =~ "email" or html =~ "Email"
      # Should have required indicator
      assert html =~ "required" or html =~ "Required"
    end

    test "does not mark optional fields as required", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/settings")

      # Optional fields should not have required indicator
      # Check that street (optional) doesn't have required badge
      # This test verifies that only required fields show the indicator
      assert html =~ "street" or html =~ "Street"
    end
  end
end
