package centralhandler

import (
	"github.com/pingcap/dbaas/billing2/forms"
	"github.com/tidbcloud/account/idl/pbgen/proto/account"
)

type CentralHandler interface {
	GetAllTenants() ([]forms.Tenant, error)
	GetTenants(ids ...uint) ([]forms.Tenant, error)
	GetTenant(tenantID uint) (*forms.Tenant, error)
	GetUser(userID uint) (*forms.User, error)
	GetUserListByOrgID(orgID uint, roles []string) ([]forms.User, error)
	GetUserListByProjectID(projectID uint, roles []string) ([]forms.User, error)
	GetMspCustomerScheduleInfo(customerID uint) (*forms.MspCustomerScheduleInfo, error)
	DownloadFile(path string) ([]byte, error)
	DeleteTenant(tenantID uint) error
	SendTenantEmailWithLabelOnce(tenantID uint, key string, templateID string, vars map[string]string, label account.MailLabels, cc []account.MailLabels) error
	SendTenantAdminEmailOnce(tenantID uint, key string, templateID string, vars map[string]string) error
	SendTenantOwnerEmailOnce(tenantID uint, key string, templateID string, vars map[string]string) error
	SendTenantManagerEmailOnce(tenantID uint, key string, templateID string, vars map[string]string) error
	SendEmail(*account.SendEmailReq) error
	ResetOnceEmailStatus(tenantID uint, key string) error
	GetProjectName(projectID uint) (string, error)
	GetProjectInfo(projectID uint) (*forms.Project, error)
	GetTenantIdByOpenAPIAccess(apiKey string) (tenantID uint64, err error)
	ListProjects(tenantID uint, containsDeleted bool) (ret []*forms.Project, err error)
	UpdateAccountThirdPartyInfo(tenantID uint, thirdPartyAccountCode string) error
	ListPartnerCustomers(tenantID uint) (ret []*forms.Customer, err error)
	IsPartnersCustomer(partnerID uint, customerID uint) (bool, error)
	ConfigFeatureFlagOrg(orgID uint64, flag string) error
	GetPendingCustomerScheduleInfo(customerID uint, partnerType account.PartnerType) ([]*forms.PartnerCustomerScheduleInfo, error)
}

type featureFlagChecker interface {
	IsHitFeatureFlag(orgID uint64, flag string) (bool, error)
}

func IsHitFeatureFlag(orgID uint64, flag string) (bool, error) {
	checker, ok := globalCentralHandler.(featureFlagChecker)
	if !ok {
		return false, nil
	}
	return checker.IsHitFeatureFlag(orgID, flag)
}

func GetCentralHandler() CentralHandler {
	return globalCentralHandler
}

// test use only.
func SetCentralHandler(handler CentralHandler) {
	globalCentralHandler = handler
}

var globalCentralHandler CentralHandler = &CentralHandlerImpl{}
