// Package api contains the TidbToolsServiceAPI interface consumed by
// `devops/pkg/controller/tidbtools.TidbToolsController`.
//
//go:generate ${GOBIN}/mockgen -destination service_mock.go -package=api ${GO_MODULE}/pkg/service/tidbtools/api TidbToolsServiceAPI
package api

import (
	"context"
	"time"

	importintong "github.com/tidbcloud/dataflow-service-ng/idl/pbgen/proto/import_into/v1beta2"
	premiumdm "github.com/tidbcloud/dataflow-service-ng/idl/pbgen/proto/premium_dm/v1beta2"
	"github.com/tidbcloud/dataflow-service/idl/pbgen/proto/dataflow"
	"github.com/tidbcloud/dataflow-service/idl/pbgen/proto/dm"
	nextgenglobal "github.com/tidbcloud/tidb-management-service/api/spec/global/nextgen/v1"
	mgmtv1 "github.com/tidbcloud/tidb-management-service/api/spec/tidb_mgmt_service/v1"
)

// PremiumRestore joins the durable restore/source metadata from
// ng-global with target instance metadata from management. Runtime counters
// remain unavailable until ng-global exposes the region restore ID required by
// cluster-service-ng.
type PremiumRestore struct {
	Status       *nextgenglobal.RestoreStatusResponse
	Target       *mgmtv1.Cluster
	RuntimeError string
	Source       *PremiumRestoreSource
	Backup       *nextgenglobal.Backup
	SourceError  string
}

type PremiumRestoreSource struct {
	ClusterID                uint64
	RestoreMode              string
	BackupID                 string
	PointInTime              *time.Time
	EventAt                  *time.Time
	UpdatedAt                *time.Time
	ExpectedRestoreDataBytes int64
	Region                   string
	CloudProvider            string
	ServicePlan              string
}

// TidbToolsServiceAPI is the surface consumed by the controller.
type TidbToolsServiceAPI interface {
	DMToolsService
	PremiumDMToolsService
	BackupRestoreToolsService
	ImportToolsService
	PremiumImportToolsService
}

// DMToolsService exposes DM jobs + precheck operations for Dedicated clusters.
type DMToolsService interface {
	ListDMJobs(ctx context.Context, orgID, projectID, clusterID uint64, page, pageSize int32) (*dm.ListJobsResp, error)
	GetDMJob(ctx context.Context, orgID, projectID, clusterID, jobID uint64) (*dm.GetJobResp, error)
	GetDMPrecheck(ctx context.Context, orgID, projectID, clusterID, precheckID uint64) (*dm.GetPrecheckJobResp, error)
	ListDMPrechecks(ctx context.Context, orgID, projectID, clusterID uint64, page, pageSize int32) (*dm.ListPrechecksResp, error)
}

// PremiumDMToolsService exposes Premium Migration (DM) operations via
// dataflow-service-ng. tidbID is the cluster ID formatted as decimal string.
type PremiumDMToolsService interface {
	ListPremiumDMJobs(ctx context.Context, tidbID string, pageSize int32) (*premiumdm.ListPremiumMigrationsResp, error)
	GetPremiumDMJob(ctx context.Context, tidbID, migrationID string) (*premiumdm.PremiumMigration, error)
	ListPremiumDMPrechecks(ctx context.Context, tidbID string, pageSize int32) (*premiumdm.ListPremiumMigrationPrechecksResp, error)
	GetPremiumDMPrecheck(ctx context.Context, tidbID, precheckID string) (*premiumdm.PremiumMigrationPrecheck, error)
}

// BackupRestoreToolsService exposes Dedicated backup/backup-export/restore
// operations and Premium backup/restore plus the distinct NextGen Export
// service. Dedicated and NextGen export methods intentionally remain separate.
type BackupRestoreToolsService interface {
	GetBackupSetting(ctx context.Context, orgID, projectID, clusterID uint64) (*dataflow.QueryBackupSettingResp, error)
	ListBackupExports(ctx context.Context, orgID, projectID, clusterID uint64, page, pageSize uint32) (*dataflow.ListBackupExportsResp, error)
	ListRestores(ctx context.Context, orgID, projectID, clusterID uint64, page, pageSize uint32) (*dataflow.ListRestoresResp, error)
	ListBackups(ctx context.Context, orgID, projectID, clusterID uint64, page, pageSize uint32) (*dataflow.Backups, error)
	ListLogBackups(ctx context.Context, orgID, projectID, clusterID uint64, page, pageSize uint32) (*dataflow.LogBackupListResp, error)
	GetRestoreProgress(ctx context.Context, orgID, projectID, clusterID uint64) (*dataflow.RestoreProgressResp, error)
	GetRestore(ctx context.Context, orgID, projectID, clusterID, restoreID uint64) (*dataflow.GetRestoreResp, error)
	ListPremiumBackups(ctx context.Context, orgID, projectID, clusterID uint64, page, pageSize int32) (*nextgenglobal.ListClusterBackupsResponse, error)
	GetPremiumBackupSetting(ctx context.Context, orgID, projectID, clusterID uint64) (*nextgenglobal.BackupSetting, *nextgenglobal.ListBackupPoliciesResponse, error)
	ListPremiumExports(ctx context.Context, orgID, projectID, clusterID uint64, page, pageSize int32) (*nextgenglobal.ListExportsResponse, error)
	GetPremiumRestoreStatus(ctx context.Context, orgID, projectID, clusterID uint64) (*nextgenglobal.RestoreStatusResponse, error)
	GetPremiumRestore(ctx context.Context, orgID, projectID, clusterID uint64) (*PremiumRestore, error)
}

// ImportToolsService exposes import operations for Dedicated clusters.
type ImportToolsService interface {
	ListImports(ctx context.Context, orgID, projectID, clusterID uint64, page, pageSize int32) (*dataflow.ListImportResp, error)
}

// PremiumImportToolsService exposes IMPORT INTO task operations for Premium clusters
// via dataflow-service-ng import_into v1beta2. tidbID is the cluster ID as decimal string.
type PremiumImportToolsService interface {
	ListImportIntoTasks(ctx context.Context, tidbID string, pageSize int32) (*importintong.ListImportIntoTasksResponse, error)
	GetImportIntoTask(ctx context.Context, tidbID, taskID string) (*importintong.ImportIntoTask, error)
}
