defmodule Credo.Check.Readability.SeparateAliasRequire do
  use Credo.Check,
    id: "EX3021",
    base_priority: :low,
    explanations: [
      check: """
      All instances of `alias` should be consecutive within a file.
      Likewise, all instances of `require` should be consecutive within a file.

      For example:

          defmodule Foo do
            require Logger
            alias Foo.Bar

            alias Foo.Baz
            require Integer

            # ...
          end

      should be changed to:

          defmodule Foo do
            require Integer
            require Logger

            alias Foo.Bar
            alias Foo.Baz

            # ...
          end

      Like all `Readability` issues, this one is not a technical concern.
      But you can improve the odds of others reading and liking your code by making
      it easier to follow.
      """
    ]

  alias Credo.Code.Block

  @doc false
  @impl true
  def run(%SourceFile{} = source_file, params \\ []) do
    ctx = Context.build(source_file, params, __MODULE__)
    result = Credo.Code.prewalk(source_file, &walk/2, ctx)
    result.issues
  end

  defp walk({:defmodule, _, _} = ast, ctx) do
    {_previous_calls, issues} =
      ast
      |> Block.calls_in_do_block()
      |> Enum.reduce({[], []}, fn
        {macro_name, meta, args}, {previous_calls, issues}
        when is_atom(macro_name) and is_list(args) ->
          cond do
            List.last(previous_calls) == macro_name ->
              {previous_calls, issues}

            macro_name in [:alias, :require] and Enum.member?(previous_calls, macro_name) ->
              {previous_calls, issues ++ [issue_for(ctx, meta, macro_name)]}

            true ->
              {previous_calls ++ [macro_name], issues}
          end

        _, memo ->
          memo
      end)

    {ast, put_issue(ctx, issues)}
  end

  defp walk(ast, ctx) do
    {ast, ctx}
  end

  defp issue_for(ctx, meta, macro_name) do
    format_issue(ctx,
      message: message(macro_name),
      trigger: macro_name,
      line_no: meta[:line]
    )
  end

  def message(:alias), do: "`alias` calls should be consecutive within a module."
  def message(:require), do: "`require` calls should be consecutive within a module."
end
