package apikey

//
//import (
//	"context"
//	"encoding/hex"
//	"sync"
//
//	usersvc "github.com/tidbcloud/account/internel/svc/user"
//
//	"github.com/tidbcloud/account/commons/database"
//	"github.com/tidbcloud/account/internel/dao/openapikey"
//
//	"github.com/pingcap/errcode"
//	"github.com/pingcap/errors"
//	"github.com/tidbcloud/account/commons/config"
//	"github.com/tidbcloud/account/idl/pbgen/proto/central"
//	"github.com/tidbcloud/account/internel/dao/entity"
//	panicIf "github.com/tidbcloud/pkgv2/panicif"
//	"github.com/tidbcloud/pkgv2/secret/encrypt"
//	"github.com/tidbcloud/tiinf/monitor/tilog"
//	"go.uber.org/zap"
//	"google.golang.org/protobuf/types/known/timestamppb"
//	"gorm.io/gorm"
//)
//
//const (
//	//publicKeyLength = 8
//	digestLength = 4
//)
//
//// Model is created with NewModel
//type OpenApiKeySvc struct {
//	log       *zap.Logger
//	encrypter *encrypt.AESCrypt
//}
//
//var (
//	openApiKeySvcInitOnce sync.Once
//	openApiKeySvc         OpenApiKeySvc
//)
//
//func GetOpenApiKeySvc() (*OpenApiKeySvc, error) {
//	openApiKeySvcInitOnce.Do(func() {
//		openApiKeySvc = NewOpenApiKeySvc()
//	})
//	return &openApiKeySvc, nil
//}
//
//// NewModel returns a Model
//func NewOpenApiKeySvc() OpenApiKeySvc {
//	log := tilog.WithContextL(context.Background())
//	gatewayConfig := config.Global().OpenAPIGatewayConfig
//	encrypter, err := encrypt.NewAESCrypt([]byte(gatewayConfig.OpenAPISecretKey))
//	if err != nil {
//		panicIf.Error(errors.Trace(err))
//	}
//
//	return OpenApiKeySvc{
//		log:       log,
//		encrypter: encrypter,
//	}
//}
//
//func (m OpenApiKeySvc) Log(ctx context.Context) *zap.Logger {
//	// TODO(iven): Move this to context.Context
//	user, err := usersvc.GetUserFromContext(ctx)
//	if err != nil {
//		panic("expected user claim")
//	}
//
//	return m.log.
//		With(
//			zap.Uint64("tenantID", user.TenantID.Uint64()),
//			zap.Uint64("userID", uint64(user.ID)),
//			zap.String("module", "openapikey"),
//		).With(tilog.TraceIDFromContext(ctx)...)
//}
//
//func (m OpenApiKeySvc) encryptPrivateKey(privateKey string) string {
//	iv := config.Global().OpenAPIGatewayConfig.OpenAPISecretIv
//	crypted := m.encrypter.EncryptWithIv([]byte(privateKey), []byte(iv))
//	return hex.EncodeToString(crypted)
//}
//
//func (m OpenApiKeySvc) decryptPrivateKey(secretKey string) (string, error) {
//	iv := config.Global().OpenAPIGatewayConfig.OpenAPISecretIv
//	crypted, err := hex.DecodeString(secretKey)
//	if err != nil {
//		return "", errors.New("failed to decode secret key as hex")
//	}
//	privateKey := m.encrypter.DecryptWithIv(crypted, []byte(iv))
//	return string(privateKey), nil
//}
//
//
//
//// fromEntity creates a OpenAPIKey model from the Database
//func (m OpenApiKeySvc) fromEntity(k entity.OpenAPIKey, includePrivateKey bool) (*central.OpenAPIKey, error) {
//	var err error
//
//	createdAt := timestamppb.New(k.CreatedAt)
//	updatedAt := timestamppb.New(*k.UpdatedAt)
//
//	apiKey := &central.OpenAPIKey{
//		Id:           k.ID.Uint64(),
//		OrgId:        k.TenantID.Uint64(),
//		PublicKey:    k.AccessKey,
//		PrivateKey:   "",
//		Digest:       k.Digest,
//		Role:         string(k.Role),
//		Description:  k.Description,
//		CreatorEmail: k.CreatorEmail,
//		CreatedAt:    createdAt,
//		UpdatedAt:    updatedAt,
//	}
//	if includePrivateKey {
//		apiKey.PrivateKey, err = m.decryptPrivateKey(k.SecretKey)
//		if err != nil {
//			return nil, errors.Trace(err)
//		}
//	}
//
//	return apiKey, nil
//}
//
//func (m OpenApiKeySvc) ListProjectOpenAPIKeys(ctx context.Context, projectID entity.ID) ([]*central.OpenAPIKey, error) {
//	var apiKeys []entity.OpenAPIKey
//
//	filter := openapikey.ProjectOpenAPIKeyFilter{
//		ProjectID: &projectID,
//	}
//
//	err := database.Transaction(func(tx *gorm.DB) error {
//		var err error
//		apiKeys, err = openapikey.ListProjectOpenAPIKeys(tx, filter)
//		return err
//	})
//	if err != nil {
//		return nil, errcode.NewInternalErr(err)
//	}
//
//	var apikeyResps []*central.OpenAPIKey
//	for _, apiKey := range apiKeys {
//		apikeyResp, err := m.fromEntity(apiKey, false)
//		if err != nil {
//			return nil, errcode.NewInternalErr(err)
//		}
//		apikeyResps = append(apikeyResps, apikeyResp)
//	}
//
//	return apikeyResps, nil
//}
