package service

import (
	"context"

	"github.com/tidbcloud/tiinf/monitor/tilog"

	apiv1 "github.com/tidbcloud/cluster-service-ng/api/tidbcloud/cluster-agent/v1"
)

func (s *AgentService) GetPublicKey(
	ctx context.Context, req *apiv1.GetPublicKeyReq) (*apiv1.GetPublicKeyResp, error) {
	logger := tilog.WithContextS(ctx).With("request", req)

	pubKey, err := s.encryptor.ExportPublicKeyAsPEM()
	if err != nil {
		logger.Error("failed to export public key", "error", err)
		return nil, err
	}
	return &apiv1.GetPublicKeyResp{
		Pem: pubKey,
	}, nil
}
