package rcuquota

import (
	"context"

	"github.com/tidbcloud/pkgv2/db"
	"go.opentelemetry.io/otel"

	globalngv1 "github.com/tidbcloud/tidb-management-service/api/spec/global/nextgen/v1"
	"github.com/tidbcloud/tidb-management-service/nextgen-global-service/domain/entity"
	"github.com/tidbcloud/tidb-management-service/nextgen-global-service/domain/repository/cluster"
	"github.com/tidbcloud/tidb-management-service/nextgen-global-service/domain/repository/rcuquota"
	"github.com/tidbcloud/tidb-management-service/utilities/bizerr"
	"github.com/tidbcloud/tidb-management-service/utilities/workflow"
)

var tracer = otel.Tracer("domain.rcuquota")

type Service interface {
	GetRcuQuota(ctx context.Context,
		request *globalngv1.GetRcuQuotaRequest) (*entity.RcuQuota, bizerr.BizError)
	CalculateMinRcu(ctx context.Context,
		request *globalngv1.CalculateMinRcuRequest) (*globalngv1.CalculateMinRcuResponse, bizerr.BizError)
	ListOrgRcuQuotas(ctx context.Context,
		request *globalngv1.ListOrgRcuQuotasRequest) ([]*entity.RcuQuota, bizerr.BizError)
	UpdateRcuQuota(ctx context.Context,
		request *globalngv1.UpdateRcuQuotaRequest) (*entity.RcuQuota, bizerr.BizError)
}

type serviceImpl struct {
	transaction  db.Transactioner
	rcuQuotaRepo rcuquota.Repo
	clusterRepo  cluster.Repo
	workflow     workflow.Workflow
}

func NewService(
	transaction db.Transactioner,
	rcuQuotaRepo rcuquota.Repo,
	clusterRepo cluster.Repo,
	flow workflow.Workflow,
) Service {
	return &serviceImpl{
		transaction:  transaction,
		rcuQuotaRepo: rcuQuotaRepo,
		clusterRepo:  clusterRepo,
		workflow:     flow,
	}
}
