defmodule Towerops.LoggerFilters do
  @moduledoc """
  Custom Logger filters to reduce noise in production logs.
  """

  @doc """
  Drops harmless Oban shutdown messages that occur during Kubernetes rolling deployments.

  These messages are expected when the old pod gracefully terminates during a deployment,
  but Oban's internal processes log them as "unexpected" even though they're normal.

  Example messages filtered:
  - "Received unexpected message: {:EXIT, #PID<...>, :shutdown}"
  - "Oban.Queue.Watchman #PID<...> received unexpected message in handle_info/2: {:EXIT, ...}"
  """
  def drop_oban_shutdown(log_event, _opts) do
    cond do
      oban_source_shutdown?(log_event) -> :stop
      oban_queue_shutdown?(log_event) -> :stop
      true -> :ignore
    end
  end

  defp oban_source_shutdown?(%{meta: %{source: :oban}, msg: {:string, msg}}) when is_binary(msg) do
    String.contains?(msg, "EXIT") and String.contains?(msg, ":shutdown")
  end

  defp oban_source_shutdown?(_), do: false

  defp oban_queue_shutdown?(%{meta: %{error_logger: %{tag: :error_msg}}, msg: {:string, msg}}) when is_binary(msg) do
    oban_component?(msg) and shutdown_message?(msg)
  end

  defp oban_queue_shutdown?(_), do: false

  defp oban_component?(msg) do
    String.contains?(msg, "Oban.Queue") or String.contains?(msg, "Oban.Plugins")
  end

  defp shutdown_message?(msg) do
    String.contains?(msg, "received unexpected message") and
      String.contains?(msg, "EXIT") and
      String.contains?(msg, ":shutdown")
  end

  @doc """
  Drops repetitive SNMP MIB resolution errors from net-snmp library.

  These errors occur when snmptranslate can't find vendor-specific MIB modules.
  The errors are logged repeatedly during polling but don't affect functionality
  since the system falls back to numeric OIDs.

  Example messages filtered:
  - "Cannot find module (TRISTAR): At line 1 in (none)"
  - "Cannot find module (VENDOR-MIB): At line 1 in (none)"
  """
  def drop_snmp_mib_errors(log_event, _opts) do
    if snmp_mib_error?(log_event), do: :stop, else: :ignore
  end

  defp snmp_mib_error?(%{msg: {:string, msg}}) when is_binary(msg) do
    String.contains?(msg, "Cannot find module") and String.contains?(msg, "At line 1 in (none)")
  end

  defp snmp_mib_error?(_), do: false

  @doc """
  Drops benign shutdown errors that occur during Kubernetes pod termination.

  These errors are expected when the BEAM shuts down inside a container:
  - `{:port_died, :normal}` - SNMP polling jobs hold UDP sockets that become invalid
  - `{:write_failed, ...} {:device, :epipe}` - Logger writes to stdout after container runtime closes the pipe
  """
  def drop_shutdown_errors(log_event, _opts) do
    if shutdown_log_message?(log_event), do: :stop, else: :ignore
  end

  defp shutdown_log_message?(%{msg: {:string, msg}}) when is_binary(msg) do
    String.contains?(msg, "port_died") or
      (String.contains?(msg, "write_failed") and String.contains?(msg, "epipe"))
  end

  defp shutdown_log_message?(_), do: false
end
