package workflow

import (
	"crypto/sha256"
	"encoding/hex"
)

const (
	DataplaneIdKey          = "dataplane_id"
	DataplaneAccountIdKey   = "dataplane_account_id"
	DataplaneCidrKey        = "dataplane_cidr"
	DataplaneServiceCidrKey = "dataplane_service_cidr"
	DataplaneNodeZonesKey   = "dataplane_node_zones"
	DataplaneHATypeKey      = "dataplane_high_availability_type"
	DataplanePCAKey         = "dataplane_pca"
	CapacityModeKey         = "capacity_mode"

	SharedPoolKey             = "shared_pool"
	EnableCapacityPlannerKey  = "enable_capacity_planner"
	SharedPoolIdKey           = "shared_pool_id"
	SourceSharedPoolIdKey     = "source_shared_pool_id"
	SharedPoolDeployTypeKey   = "shared_pool_deploy_type"
	PDDeploymentModeKey       = "pd_deployment_mode"
	TiDBGroupInitSQLKey       = "tidb_group_init_sql"
	TiDBGroupKey              = "tidb_group"
	TiDBGroupIdKey            = "tidb_group_id"
	AutoscalerTemplateArgsKey = "autoscaler_template_args"
	TiDBClusterKey            = "tidb_cluster"
	TiDBClusterIdKey          = "tidb_cluster_id"
	TenantIdKey               = "tenant_id"
	ComponentIdsKey           = "component_ids"
	ComponentTypesKey         = "component_types"
	CmekIdKey                 = "cmek_id"

	PauseResumeOperationIdKey   = "pause_resume_operation_id"
	PauseResumeOperationTypeKey = "pause_resume_operation_type"

	// prune dataplane
	PruneDataplaneKey   = "prune_dataplane"
	PruneDataplaneValue = "true"

	// pause cluster
	// PauseClusterKey   = "pause_cluster"
	// PauseClusterValue = "true"

	// DM Cluster
	DMClusterIdKey = "dm_cluster_id"
	DMClusterKey   = "dm_cluster"
	DMBackendKey   = "dm_backend"

	DMDeleteKey   = "dm_delete"
	DMDeleteValue = "true"

	DMUpdateImageKey = "dm_update_image"

	// updateLoadBalancer
	UpdateLoadBalancerKey   = "update_load_balancer"
	UpdateLoadBalancerValue = "true"

	// force scale
	ForceKey   = "force"
	ForceValue = "true"

	// update strategy
	UpdateStrategyKey = "update_strategy"

	// cluster upgrade policy
	EnsureNoConstraintsUpgradePolicyKey = "ensure_no_constraints_upgrade_policy"

	// update node version
	NodeVersionKey = "node_version"

	// tici
	EnableTiCIKey   = "enable_tici"
	EnableTiCIValue = "true"

	// skip shared tiproxy apply
	SkipSharedTiProxyApplyKey   = "skip_shared_tiproxy_apply"
	SkipSharedTiProxyApplyValue = "true"

	// skip component HPA apply when a DAG has no component mutation target
	SkipComponentHPAApplyKey   = "skip_component_hpa_apply"
	SkipComponentHPAApplyValue = "true"

	// tiproxy external dns
	EnableExternalDNSKey = "enable_external_dns"
	ExternalDNSTTLKey    = "external_dns_ttl"

	// config center
	ConfigCenterMetaProfileIdKey = "config_center_meta_profile_id"

	// backup & restore
	RestoreIdKey       = "restore_id"
	TiBRMemoryKey      = "tibr_memory"
	TiBRCPUKey         = "tibr_cpu"
	TiBRStorageKey     = "tibr_storage"
	PDClusterIdKey     = "pd_cluster_id"
	TargetKeyspaceKey  = "target_keyspace"
	SourceKeyspaceKey  = "source_keyspace"
	BackupIdKey        = "backup_id"
	BackupNameKey      = "backup_name"
	PointInTimeKey     = "point_in_time"
	ManualBackupIdKey  = "manual_backup_id"
	ManualRestoreIdKey = "manual_restore_id"

	// export
	ExportIdKey      = "export_id"
	ExportClusterKey = "export_cluster_id"
	ExportKey        = "export"

	// metering sending type
	MeteringTypeKey                         = "metering_type"
	MeteringTypeValueLogicMetaByTiDBCluster = "logic_meta_by_tidb_cluster"
	MeteringTypeValueLogicMetaBySharedPool  = "logic_meta_by_shared_pool"
	MeteringTypeValueSharedPoolMeta         = "shared_pool_meta"

	MeteringDeleteTiDBClusterKey   = "metering_delete_tidb_cluster"
	MeteringDeleteTiDBClusterValue = "true"

	MeteringDeleteSharedPoolKey   = "metering_delete_shared_pool"
	MeteringDeleteSharedPoolValue = "true"
	MeteringModifyTSKey           = "metering_modify_ts"

	// node instance group action args
	NodeInstanceGroupKey     = "node_instance_group"
	PlacementGroupEnabledKey = "placement_group_enabled"
	InstanceTypeKey          = "instance_type"
	InstanceTypeStatusKey    = "instance_type_status"

	// affected component types for overlay
	AffectedComponentTypesKey = "affected_component_types"

	// target tikv remote worker routing
	EnabledTiKVRemoteWorkersKey = "enabled_tikv_remote_workers"
)

func PauseResumeExecID(sharedPoolID, operationID, operationType string) string {
	digest := sha256.Sum256([]byte(sharedPoolID + "\x00" + operationID + "\x00" + operationType))
	return "pause-resume-" + hex.EncodeToString(digest[:16])
}

const TiKVPlacementExclusiveKey = "tikv_placement_exclusive"
