defmodule Berrypod.Providers do
  @moduledoc """
  Convenience functions for working with POD providers.
  """

  alias Berrypod.Products.ProviderConnection
  alias Berrypod.Providers.Provider

  @doc """
  Tests a provider connection.

  Returns `{:ok, info}` with provider-specific info (e.g., shop name, shop_id)
  or `{:error, reason}` if the connection fails.
  """
  def test_connection(%ProviderConnection{} = conn) do
    case Provider.for_connection(conn) do
      {:ok, provider} -> provider.test_connection(conn)
      {:error, :not_implemented} -> {:error, :provider_not_implemented}
      error -> error
    end
  end

  @doc """
  Returns the provider module for a given type.
  """
  defdelegate for_type(type), to: Provider
end
