package migrationcluster

import (
	"context"

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

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

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

type Service interface {
	CreateMigrationCluster(
		ctx context.Context,
		req *tidbmgmtservicev1.CreateMigrationClusterRequest,
		projectID uint64,
		sourceCluster *entity.Cluster,
	) (*tidbmgmtservicev1.MigrationCluster, error)
	GetMigrationCluster(
		ctx context.Context,
		req *tidbmgmtservicev1.GetMigrationClusterRequest,
	) (*tidbmgmtservicev1.MigrationCluster, error)
}

type service struct {
	migrationClusterRepo repository.MigrationClusterRepo
	clusterRepo          repository.ClusterRepository
	dedicatedCli         dedicatedglobal.DedicatedGlobal
	transaction          db.TransProvider
	workflowSvc          workflow.Workflow
}

func NewService(
	migrationClusterRepo repository.MigrationClusterRepo,
	clusterRepo repository.ClusterRepository,
	dedicatedCli dedicatedglobal.DedicatedGlobal,
	transaction db.TransProvider,
	workflowSvc workflow.Workflow,
) Service {
	svc := &service{
		migrationClusterRepo: migrationClusterRepo,
		clusterRepo:          clusterRepo,
		dedicatedCli:         dedicatedCli,
		transaction:          transaction,
		workflowSvc:          workflowSvc,
	}

	return svc
}
