 
   defp nerves_br_version_to_metadata(nerves_br_version) do
     [
-      Task.async(API, :fetch_buildroot_version!, [nerves_br_version]),
-      Task.async(API, :fetch_otp_version!, [nerves_br_version])
+      Task.async(NervesSystemCompatibility.API, :fetch_buildroot_version!, [nerves_br_version]),
+      Task.async(NervesSystemCompatibility.API, :fetch_otp_version!, [nerves_br_version])
     ]
     |> Task.await_many(:timer.seconds(10))
     |> Enum.reduce(%{"nerves_br_version" => nerves_br_version}, fn %{} = data, acc ->

end

defmodule NervesSystemCompatibility.Table do
  alias NervesSystemCompatibility.Data

  @doc """
  Converts the compatibility data to a markdown table.
  """
  @spec build([%{binary => any}]) :: binary
  def build(compatibility_data) do
    targets = NervesSystemCompatibility.target_systems()
    otp_versions = NervesSystemCompatibility.otp_versions()

    [
      header_row(targets),
      divider_row(targets),
      data_rows(targets, otp_versions, compatibility_data)
    ]
    |> Enum.join("\n")
  end

  defp header_row(targets) when is_list(targets) do
    [
      "|",
      [cell("", 12) | Enum.map(targets, &cell/1)] |> Enum.intersperse("|"),
      "|"
    ]
    |> Enum.join()
  end

  defp divider_row(targets) when is_list(targets) do
    [
      "|",
      [cell("---", 12) | List.duplicate(cell("---"), length(targets))] |> Enum.intersperse("|"),
      "|"
    ]
    |> Enum.join()
  end

  defp data_rows(targets, otp_versions, compatibility_data) do
    grouped_by_otp_and_target = compatibility_data |> Data.group_data_by_otp_and_target()

    for otp_version <- otp_versions, reduce: [] do
      acc ->
        target_versions =
          for target <- targets do
            get_in(grouped_by_otp_and_target, [otp_version, target, "target_version"])
          end

        [data_row(otp_version, target_versions) | acc]
    end
    |> Enum.join("\n")
  end

  defp data_row(otp_version, row_values) when is_list(row_values) do
    [
      "|",
      [cell("OTP #{otp_version}", 12) | Enum.map(row_values, &cell/1)] |> Enum.intersperse("|"),
      "|"
    ]
    |> Enum.join()
  end

  defp cell(value, count \\ 10) do
    (" " <> to_string(value)) |> String.pad_trailing(count)
  end
end


    end)
  end

  @spec group_data_by_target(compatibility_data) :: %{binary => %{atom => any}}
  def group_data_by_target(compatibility_data) do
    compatibility_data
    |> Enum.group_by(&Map.fetch!(&1, "target"))
    |> Map.new(fn {target, target_entries} ->
      {
        target,
        target_entries
        |> Enum.reject(&String.match?(&1["target_version"], ~r/-rc/))
        |> Enum.group_by(&Map.fetch!(&1, "target_version"))
        |> Map.new(fn {target_version, target_version_entries} ->
          {
            target_version,
            target_version_entries
            |> Enum.max_by(&normalize_version(&1["target_version"]), Version)
          }
        end)
      }
    end)
  end

  @spec filter_by(compatibility_data, any, any) :: compatibility_data
  def filter_by(compatibility_data, key, value) do
    compatibility_data |> Enum.filter(&Kernel.==(&1[key], value))
  end

  @spec list_target_system_versions(compatibility_data, atom) :: [binary]
  def list_target_system_versions(compatibility_data, target) do
    compatibility_data
    |> filter_by("target", target)
    |> Enum.map(&Access.fetch!(&1, "target_version"))
    |> Enum.reject(&String.match?(&1, ~r/-rc/))
    |> Enum.uniq()
    |> Enum.sort({:desc, Version})
  end

  defp normalize_version(version) do
    case version |> String.split(".") |> Enum.count(&String.to_integer/1) do
      1 -> version <> ".0.0"
      2 -> version <> ".0"
