defmodule MvWeb.DatafieldsLive do
  @moduledoc """
  LiveView for managing member field visibility/required and custom fields (datafields).

  Renders MemberFieldLive.IndexComponent and CustomFieldLive.IndexComponent.
  Moved from GlobalSettingsLive (Memberdata section) to a dedicated page.
  """
  use MvWeb, :live_view

  alias Mv.Membership

  on_mount {MvWeb.LiveHelpers, :ensure_user_role_loaded}

  @impl true
  def mount(_params, _session, socket) do
    {:ok, settings} = Membership.get_settings()

    {:ok,
     socket
     |> assign(:page_title, gettext("Datafields"))
     |> assign(:settings, settings)
     |> assign(:active_editing_section, nil)}
  end

  @impl true
  def render(assigns) do
    ~H"""
    <Layouts.app flash={@flash} current_user={@current_user} club_name={@settings.club_name}>
      <.header>
        {gettext("Datafields")}
        <:subtitle>
          {gettext("Configure member fields and custom data fields.")}
        </:subtitle>
      </.header>

      <.form_section title={gettext("Member fields")}>
        <.live_component
          :if={@active_editing_section != :custom_fields}
          module={MvWeb.MemberFieldLive.IndexComponent}
          id="member-fields-component"
          settings={@settings}
        />
      </.form_section>

      <.form_section title={gettext("Custom fields")}>
        <.live_component
          :if={@active_editing_section != :member_fields}
          module={MvWeb.CustomFieldLive.IndexComponent}
          id="custom-fields-component"
          actor={@current_user}
        />
      </.form_section>
    </Layouts.app>
    """
  end

  @impl true
  def handle_info({:custom_field_saved, _custom_field, action}, socket) do
    send_update(MvWeb.CustomFieldLive.IndexComponent,
      id: "custom-fields-component",
      show_form: false
    )

    {:noreply,
     socket
     |> assign(:active_editing_section, nil)
     |> put_flash(:success, gettext("Data field %{action} successfully", action: action))}
  end

  @impl true
  def handle_info({:custom_field_deleted, _custom_field}, socket) do
    {:noreply, put_flash(socket, :success, gettext("Data field deleted successfully"))}
  end

  @impl true
  def handle_info({:custom_field_delete_error, error}, socket) do
    {:noreply,
     put_flash(
       socket,
       :error,
       gettext("Failed to delete data field: %{error}", error: inspect(error))
     )}
  end

  @impl true
  def handle_info(:custom_field_slug_mismatch, socket) do
    {:noreply, put_flash(socket, :error, gettext("Slug does not match. Deletion cancelled."))}
  end

  def handle_info({:custom_fields_load_error, _error}, socket) do
    {:noreply,
     put_flash(
       socket,
       :error,
       gettext("Could not load data fields. Please check your permissions.")
     )}
  end

  @impl true
  def handle_info({:editing_section_changed, section}, socket) do
    {:noreply, assign(socket, :active_editing_section, section)}
  end

  # Open delete modal for custom field (triggered from Danger zone in FormComponent)
  @impl true
  def handle_info({:open_delete_modal_for, custom_field}, socket) do
    send_update(MvWeb.CustomFieldLive.IndexComponent,
      id: "custom-fields-component",
      open_delete_for_id: custom_field.id
    )

    {:noreply, socket}
  end

  @impl true
  def handle_info({:member_field_saved, _member_field, action}, socket) do
    {:ok, updated_settings} = Membership.get_settings()

    send_update(MvWeb.MemberFieldLive.IndexComponent,
      id: "member-fields-component",
      show_form: false,
      settings: updated_settings
    )

    {:noreply,
     socket
     |> assign(:settings, updated_settings)
     |> assign(:active_editing_section, nil)
     |> put_flash(:success, gettext("Member field %{action} successfully", action: action))}
  end

  @impl true
  def handle_info({:member_field_visibility_updated}, socket) do
    {:ok, updated_settings} = Membership.get_settings()

    send_update(MvWeb.MemberFieldLive.IndexComponent,
      id: "member-fields-component",
      settings: updated_settings
    )

    {:noreply, assign(socket, :settings, updated_settings)}
  end
end
