package ibm

import (
	"fmt"

	"github.com/IBM/platform-services-go-sdk/globalcatalogv1"
)

// GlobalCatalogClient represents a client for interacting with IBM Cloud Global Catalog
type GlobalCatalogClient struct {
	client *globalcatalogv1.GlobalCatalogV1
}

// NewGlobalCatalogClient creates a new Global Catalog client
func NewGlobalCatalogClient(iamClient *IAMClient) *GlobalCatalogClient {
	authenticator := iamClient.GetAuthenticator()
	client, err := globalcatalogv1.NewGlobalCatalogV1(&globalcatalogv1.GlobalCatalogV1Options{
		Authenticator: authenticator,
	})
	if err != nil {
		// Since this is during initialization, we'll panic if we can't create the client
		panic(fmt.Sprintf("failed to create global catalog client: %v", err))
	}

	return &GlobalCatalogClient{
		client: client,
	}
}

// GetEntry retrieves a catalog entry by ID
func (c *GlobalCatalogClient) GetEntry(id string) (*globalcatalogv1.CatalogEntry, error) {
	entry, _, err := c.client.GetCatalogEntry(&globalcatalogv1.GetCatalogEntryOptions{
		ID: &id,
	})
	if err != nil {
		return nil, fmt.Errorf("getting catalog entry: %w", err)
	}
	return entry, nil
}
