defmodule Hexpm.Application do
  use Application

  def start(_type, _args) do
    :logger.add_handler(:sentry_handler, Sentry.LoggerHandler, %{})

    topologies = cluster_topologies()
    read_only_mode()
    Hexpm.BlockAddress.start()

    children = [
      Hexpm.RepoBase,
      {Finch, name: Hexpm.Finch},
      {Task.Supervisor, name: Hexpm.Tasks},
      {Cluster.Supervisor, [topologies, [name: Hexpm.ClusterSupervisor]]},
      {Phoenix.PubSub, name: Hexpm.PubSub, adapter: Phoenix.PubSub.PG2},
      HexpmWeb.RateLimitPubSub,
      {PlugAttack.Storage.Ets, name: HexpmWeb.Plugs.Attack.Storage, clean_period: 60_000},
      {Hexpm.Billing.Report, name: Hexpm.Billing.Report, interval: 60_000},
      goth_spec(),
      setup(),
      HexpmWeb.Telemetry,
      HexpmWeb.Endpoint
    ]

    File.mkdir_p(Application.get_env(:hexpm, :tmp_dir))
    shutdown_on_eof()

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

  def config_change(changed, _new, removed) do
    HexpmWeb.Endpoint.config_change(changed, removed)
    :ok
  end

  def sentry_before_send(%Sentry.Event{original_exception: exception} = event) do
    cond do
      Plug.Exception.status(exception) < 500 -> nil
      Sentry.DefaultEventFilter.exclude_exception?(exception, event.source) -> nil
      true -> event
    end
  end

  # Make sure we exit after hex client tests are finished running
  if Mix.env() == :hex do
    def shutdown_on_eof() do
      spawn_link(fn ->
        IO.gets(:stdio, ~c"") == :eof && System.halt(0)
      end)
    end
  else
    def shutdown_on_eof(), do: nil
  end

  defp read_only_mode() do
    mode = System.get_env("HEXPM_READ_ONLY_MODE") == "1"
    Application.put_env(:hexpm, :read_only_mode, mode)
  end

  defp setup() do
    fun = fn ->
      if System.get_env("HEXPM_SETUP") == "1" do
        Hexpm.setup()
      end
    end

    %{
      id: :task_setup,
      start: {Task, :start_link, [fun]},
      restart: :temporary
    }
  end

  defp cluster_topologies() do
    if System.get_env("HEXPM_CLUSTER") == "1" do
      Application.get_env(:hexpm, :topologies) || []
    else
      []
    end
  end

  if Mix.env() == :prod do
    defp goth_spec() do
      credentials =
        "HEXPM_GCP_CREDENTIALS"
        |> System.fetch_env!()
        |> Jason.decode!()

      options = [scope: "https://www.googleapis.com/auth/devstorage.read_write"]
      {Goth, name: Hexpm.Goth, source: {:service_account, credentials, options}}
    end
  else
    defp goth_spec() do
      {Task, fn -> :ok end}
    end
  end
end
