package privatelinkservicemigration

import (
	"context"

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

	globaldedicatedv1 "github.com/tidbcloud/tidb-management-service/api/spec/global/dedicated/v1"
	"github.com/tidbcloud/tidb-management-service/dedicated-global-service/domain/adapter/regionserver"
	"github.com/tidbcloud/tidb-management-service/dedicated-global-service/domain/repository/cluster"
	"github.com/tidbcloud/tidb-management-service/dedicated-global-service/domain/repository/privatelinkservicemigration"
	"github.com/tidbcloud/tidb-management-service/dedicated-global-service/domain/repository/region"
	"github.com/tidbcloud/tidb-management-service/dedicated-global-service/domain/repository/tidbgroup"
	"github.com/tidbcloud/tidb-management-service/utilities/idgenerator"
	"github.com/tidbcloud/tidb-management-service/utilities/workflow"
)

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

type Service interface {
	Create(
		ctx context.Context,
		req *globaldedicatedv1.CreatePrivateLinkServiceMigrationRequest,
	) (*globaldedicatedv1.PrivateLinkServiceMigration, error)
	Get(ctx context.Context, req *globaldedicatedv1.GetPrivateLinkServiceMigrationRequest) (*globaldedicatedv1.PrivateLinkServiceMigration, error)
	List(
		ctx context.Context,
		req *globaldedicatedv1.ListPrivateLinkServiceMigrationsRequest,
	) ([]*globaldedicatedv1.PrivateLinkServiceMigration, error)
	Cancel(ctx context.Context, req *globaldedicatedv1.CancelPrivateLinkServiceMigrationRequest) error
}

func NewService(
	privatelinkservicemigrationRepo privatelinkservicemigration.Repo,
	clusterRepo cluster.Repo,
	tidbGroupRepo tidbgroup.Repo,
	regionRepo region.Repo,
	regionserverCli regionserver.Client,
	wflow workflow.Workflow,
	idGeneratorFacade idgenerator.IDGenerator,
	transaction db.Transactioner,
) Service {
	return &service{
		privatelinkservicemigrationRepo: privatelinkservicemigrationRepo,
		clusterRepo:                     clusterRepo,
		tidbGroupRepo:                   tidbGroupRepo,
		regionRepo:                      regionRepo,
		regionserverCli:                 regionserverCli,
		workflow:                        wflow,
		idGeneratorFacade:               idGeneratorFacade,
		transaction:                     transaction,
	}
}

type service struct {
	privatelinkservicemigrationRepo privatelinkservicemigration.Repo
	clusterRepo                     cluster.Repo
	tidbGroupRepo                   tidbgroup.Repo
	regionRepo                      region.Repo
	regionserverCli                 regionserver.Client
	idGeneratorFacade               idgenerator.IDGenerator
	transaction                     db.Transactioner
	workflow                        workflow.Workflow
}
