defmodule ExploitGuard.Application do
  @moduledoc false

  use Application
  require Logger

  @impl true
  def start(_type, _args) do
    children =
      case Application.get_env(:exploit_guard, :mode) do
        nil ->
          Logger.info(
            "[Exploit Guard] No configuration found, tracing disabled. Valid options are :monitor or :block"
          )

          []

        mode when mode in [:monitor, :block] ->
          Logger.info("[Exploit Guard] Running in monitor mode.")
          handler = Application.get_env(:exploit_guard, :handler)
          :persistent_term.put(:mode, mode)

          [
            {Task.Supervisor, name: ExploitGuard.TaskSupervisor},
            {ExploitGuard.Watcher, %{handler: handler}}
          ]

        _other ->
          Logger.info(
            "[Exploit Guard] Bad configuration, tracing disabled. Valid options are :monitor or :block"
          )

          []
      end

    opts = [strategy: :one_for_one, name: ExploitGuard.Supervisor]
    Supervisor.start_link(children, opts)
  end
end
