package gorgias

import (
	"context"
	"fmt"
	"net/http"

	"github.com/amp-labs/connectors/common"
	"github.com/amp-labs/connectors/common/scanning/credscanning"
	"github.com/amp-labs/connectors/providers"
	"github.com/amp-labs/connectors/providers/gorgias"
	"github.com/amp-labs/connectors/test/utils"
	"golang.org/x/oauth2"
)

func GetConnector(ctx context.Context) *gorgias.Connector {
	filePath := credscanning.LoadPath(providers.Gorgias)
	reader := utils.MustCreateProvCredJSON(filePath, true)

	client, err := common.NewOAuthHTTPClient(ctx,
		common.WithOAuthClient(http.DefaultClient),
		common.WithOAuthConfig(getConfig(reader)),
		common.WithOAuthToken(reader.GetOauthToken()),
	)
	if err != nil {
		utils.Fail(err.Error())
	}

	conn, err := gorgias.NewConnector(common.ConnectorParams{
		AuthenticatedClient: client,
		Workspace:           "ampersand",
	})
	if err != nil {
		utils.Fail("error creating connector", "error", err)
	}

	return conn
}

func getConfig(reader *credscanning.ProviderCredentials) *oauth2.Config {
	cfg := oauth2.Config{
		ClientID:     reader.Get(credscanning.Fields.ClientId),
		ClientSecret: reader.Get(credscanning.Fields.ClientSecret),
		Endpoint: oauth2.Endpoint{
			AuthURL:   fmt.Sprintf("https://%s.gorgias.com/oauth/authorize", "ampersand"),
			TokenURL:  fmt.Sprintf("https://%s.gorgias.com/oauth/token", "ampersand"),
			AuthStyle: oauth2.AuthStyleAutoDetect,
		},
		Scopes: []string{
			"offline", "write:all",
		},
	}

	return &cfg
}
