defmodule Mv.Constants do
  @moduledoc """
  Module for defining constants and atoms.
  """

  @member_fields [
    :first_name,
    :last_name,
    :email,
    :join_date,
    :exit_date,
    :notes,
    :country,
    :city,
    :street,
    :house_number,
    :postal_code,
    :membership_fee_start_date
  ]

  @custom_field_prefix "custom_field_"

  @boolean_filter_prefix "bf_"

  @max_boolean_filters 50

  @max_uuid_length 36

  @email_validator_checks [:html_input, :pow]

  # Member fields that are required when Vereinfacht integration is active (contact sync)
  @vereinfacht_required_member_fields [:first_name, :last_name, :street, :postal_code, :city]

  def member_fields, do: @member_fields

  @doc """
  Returns member fields that are always required when Vereinfacht integration is configured.

  Used for validation, member form required indicators, and settings UI (checkbox disabled).
  """
  def vereinfacht_required_member_fields, do: @vereinfacht_required_member_fields

  @doc """
  Returns whether the given member field is required by Vereinfacht when integration is active.
  """
  def vereinfacht_required_field?(field) when is_atom(field),
    do: field in @vereinfacht_required_member_fields

  def vereinfacht_required_field?(_), do: false

  @doc """
  Returns the prefix used for custom field keys in field visibility maps.

  ## Examples

      iex> Mv.Constants.custom_field_prefix()
      "custom_field_"
  """
  def custom_field_prefix, do: @custom_field_prefix

  @doc """
  Returns the prefix used for boolean custom field filter URL parameters.

  ## Examples

      iex> Mv.Constants.boolean_filter_prefix()
      "bf_"
  """
  def boolean_filter_prefix, do: @boolean_filter_prefix

  @doc """
  Returns the maximum number of boolean custom field filters allowed per request.

  This limit prevents DoS attacks by restricting the number of filter parameters
  that can be processed in a single request.

  ## Examples

      iex> Mv.Constants.max_boolean_filters()
      50
  """
  def max_boolean_filters, do: @max_boolean_filters

  @doc """
  Returns the maximum length of a UUID string (36 characters including hyphens).

  UUIDs in standard format (e.g., "550e8400-e29b-41d4-a716-446655440000") are
  exactly 36 characters long. This constant is used for input validation.

  ## Examples

      iex> Mv.Constants.max_uuid_length()
      36
  """
  def max_uuid_length, do: @max_uuid_length

  @doc """
  Returns the email validator checks used for EctoCommons.EmailValidator.

  We use both `:html_input` and `:pow` checks:
  - `:html_input` - Pragmatic validation matching browser `<input type="email">` behavior
  - `:pow` - Stricter validation following email spec, supports internationalization (Unicode)

  Using both ensures:
  - Compatibility with common email providers (html_input)
  - Compliance with email standards (pow)
  - Support for international email addresses (pow)

  ## Examples

      iex> Mv.Constants.email_validator_checks()
      [:html_input, :pow]
  """
  def email_validator_checks, do: @email_validator_checks
end
