package pkg

import (
	"github.com/tidbcloud/debug_portal/config"
	"github.com/tidbcloud/debug_portal/idl/pbgen/debug_portal"
	"github.com/tidbcloud/debug_portal/model"
	"github.com/tidbcloud/debug_portal/session"
	"golang.org/x/oauth2"
	"strings"
)

var _ debug_portal.CentralDebugPortalServiceServer = (*debugPortalService)(nil)

func NewService(
	s OperationsService,
	model model.Model,
	debugPortalConfig *config.DebugPortalConfig,
	debugSessionManager *session.SessionManager,
) debug_portal.CentralDebugPortalServiceServer {
	oauthConfig := oauth2.Config{
		ClientID:     debugPortalConfig.OAuthClientID,
		ClientSecret: debugPortalConfig.OAuthClientSecret,
		Scopes:       strings.Split(debugPortalConfig.OAuthScopes, " "),
		Endpoint: oauth2.Endpoint{
			AuthURL:  debugPortalConfig.OAuthAuthorizationURL,
			TokenURL: debugPortalConfig.OAuthTokenURL,
		},
	}
	return &debugPortalService{
		OperationsService: s,
		model:             model,
		config:            debugPortalConfig,
		oAuthClient:       session.NewOAuthClient(oauthConfig, debugPortalConfig.OAuthUserInfoURL),
		sessionManager:    debugSessionManager,
	}
}
