package dedicatedapi

import (
	"fmt"
	"strings"

	"github.com/icholy/digest"
	"github.com/stretchr/testify/suite"

	"github.com/tidbcloud/observability/test/ft/common/dedicatedapi/api/client"
)

type Cli struct {
	su *suite.Suite

	*client.APIClient
}

func NewClient(su *suite.Suite, host, apiKey string) *Cli {
	cfg := client.NewConfiguration()
	// Enable debug request log.
	cfg.Debug = true
	// Set the server URL. Notice the base path is not included in the host.
	cfg.Servers = client.ServerConfigurations{
		{
			URL: fmt.Sprintf("https://%s/v1beta1", host),
		},
	}

	apiClient := client.NewAPIClient(cfg)

	split := strings.Split(apiKey, ":")

	// Set the digest auth.
	cfg.HTTPClient.Transport = &digest.Transport{
		Username: split[0],
		Password: split[1],
	}

	return &Cli{su: su, APIClient: apiClient}
}
