package resourcepool

import (
	"context"

	"github.com/tidbcloud/pkgv2/db"

	tidbmgmtservicev1 "github.com/tidbcloud/tidb-management-service/api/spec/tidb_mgmt_service/v1"
	"github.com/tidbcloud/tidb-management-service/tidb-mgmt-service/domain/adapter/nextgenglobal"
	"github.com/tidbcloud/tidb-management-service/tidb-mgmt-service/domain/entity"
	"github.com/tidbcloud/tidb-management-service/tidb-mgmt-service/domain/repository"
)

type pauseCustomStatusRepository interface {
	LockClustersForUpdate(ctx context.Context, clusterIds []uint64) error
	ListClusterCustomStatus(
		ctx context.Context,
		clusterIds []uint64,
		statusTypes []tidbmgmtservicev1.CustomStatusType,
		queryDeleted bool,
	) (map[uint64][]*entity.ClusterCustomStatus, error)
}

func NewServer(
	nextgenCli nextgenglobal.NextGenGlobal,
	clusterRepo repository.ClusterRepository,
	customStatusRepo pauseCustomStatusRepository,
	transaction db.Transactioner,
) tidbmgmtservicev1.ResourcePoolServiceServer {
	return &serverImpl{
		nextgenCli:       nextgenCli,
		clusterRepo:      clusterRepo,
		customStatusRepo: customStatusRepo,
		transaction:      transaction,
	}
}

type serverImpl struct {
	tidbmgmtservicev1.UnimplementedResourcePoolServiceServer

	nextgenCli       nextgenglobal.NextGenGlobal
	clusterRepo      repository.ClusterRepository
	customStatusRepo pauseCustomStatusRepository
	transaction      db.Transactioner
}
