defmodule CentralcloudCore.DrPortal do
  @moduledoc """
  DR Portal API client (Go/Gin backend at dr.centralcloud.com).
  Used by my.centralcloud.com to fetch VM/replication status for a customer.
  """

  @spec get_customer_vms(String.t()) :: {:ok, list(map())} | {:error, term()}
  def get_customer_vms(customer_id) do
    get("/api/v1/customers/#{customer_id}/vms")
  end

  @spec get_replication_status(String.t()) :: {:ok, map()} | {:error, term()}
  def get_replication_status(customer_id) do
    get("/api/v1/customers/#{customer_id}/replication")
  end

  # ---------------------------------------------------------------------------
  # Private

  defp get(path) do
    cfg = Application.fetch_env!(:centralcloud_core, :dr_portal)
    url = cfg[:base_url] <> path

    case Req.get(url, headers: [{"x-api-key", cfg[:api_key]}]) do
      {:ok, %{status: 200, body: body}} -> {:ok, body}
      {:ok, %{status: status}}          -> {:error, {:http, status}}
      {:error, reason}                  -> {:error, reason}
    end
  end
end
