lib/qr_nbu/validators/recipient_code.ex

defmodule QRNBU.Validators.RecipientCode do
  @moduledoc """
  Validator for recipient identification code (EDRPOU/IPN/passport).

  Validates Ukrainian business (EDRPOU) and individual (IPN) tax identification codes.
  """

  @doc """
  Validates recipient code.

  ## Rules
  - Must be 8 or 10 digits for EDRPOU/IPN
  - Can be any alphanumeric format for passport numbers

  ## Examples

      iex> QRNBU.Validators.RecipientCode.validate("12345678")
      {:ok, "12345678"}

      iex> QRNBU.Validators.RecipientCode.validate("1234567890")
      {:ok, "1234567890"}

      iex> QRNBU.Validators.RecipientCode.validate("AB123456")
      {:ok, "AB123456"}

      iex> QRNBU.Validators.RecipientCode.validate("")
      {:error, "Recipient code is required"}

      iex> QRNBU.Validators.RecipientCode.validate("123")
      {:error, "Recipient code must be 8-10 digits or valid passport format"}
  """
  @spec validate(String.t()) :: {:ok, String.t()} | {:error, String.t()}
  def validate(code) when is_binary(code) do
    cond do
      String.trim(code) == "" ->
        {:error, "Recipient code is required"}

      String.length(code) < 6 ->
        {:error, "Recipient code must be at least 6 characters"}

      String.length(code) > 35 ->
        {:error, "Recipient code must not exceed 35 characters"}

      true ->
        {:ok, code}
    end
  end

  def validate(_), do: {:error, "Recipient code must be a string"}
end