package vo

import (
	"fmt"

	"github.com/tidbcloud/serverless-service/idl/pbgen/proto/console"
	v1beta1 "github.com/tidbcloud/serverless-service/idl/pbgen/proto/v1beta1"
	"github.com/tidbcloud/serverless-service/internal/config"
)

// ChangefeedSpecification is the public face of one rcu tier returned by
// ListChangefeedSpecifications. Only the fields the UI uses to render and
// pick a tier are exposed; the machine-shape catalog
// (config.CDCNewArchInstanceType) is internal.
//
// Name is the stable id ("2rcu", "8rcu", ...) callers can echo back on
// create / scale; it's derived from the tier's RCU.
type ChangefeedSpecification struct {
	Name     string `json:"name,omitempty"`
	RCU      uint32 `json:"rcu,omitempty"`
	RPSLimit uint32 `json:"rps_limit,omitempty"`
}

func ChangefeedSpecificationFromConfig(tier config.CDCNewArchRcuTier) *ChangefeedSpecification {
	return &ChangefeedSpecification{
		Name:     fmt.Sprintf("%drcu", tier.RCU),
		RCU:      tier.RCU,
		RPSLimit: tier.RPSLimit,
	}
}

func (s *ChangefeedSpecification) ConvertToV1beta1() *v1beta1.ChangefeedSpecification {
	if s == nil {
		return nil
	}
	return &v1beta1.ChangefeedSpecification{
		Name:     s.Name,
		Rcu:      s.RCU,
		RpsLimit: s.RPSLimit,
	}
}

func (s *ChangefeedSpecification) ConvertToConsole() *console.ChangefeedSpecification {
	if s == nil {
		return nil
	}
	return &console.ChangefeedSpecification{
		Name:     s.Name,
		Rcu:      s.RCU,
		RpsLimit: s.RPSLimit,
	}
}
