package consts

import (
	"time"

	"github.com/tidbcloud/pkgv2/auth/auth0"
	"github.com/tidbcloud/pkgv2/auth/util"
	"github.com/tidbcloud/tiinf/sdk/rbac/common"
)

// common const
const (
	DefaultDBOperationTimeout = 30 * time.Second

	// cluster proxy
	DefaultClusterProxyTimeout = 30
	// for cdc create and update request, cdc need to list all the tables, and it can spend much time
	// so we need a longer timeout
	ClusterProxyCDCHeavyOperationTimeout = 60 * 5
)

// backup const
const (
	// TODO add a column to indicate whether a backup is a final backup
	FinalBackupName                         = "final-backup"
	ServerlessDefaultBackupTime             = "05:00" // UTC
	RegionDefaultBackupTimeFormat           = "15:04"
	MaxAutoBackups                   uint64 = 7
	DefaultRetentionPeriod           uint64 = 7
	ServerlessMaxAutoBackups         uint64 = 31
	ServerlessDefaultRetentionPeriod uint64 = 31
	ServerlessMinRetentionPeriod     uint32 = 7
	ServerlessMaxRetentionPeriod     uint32 = 90
	DefaultAutoBackupHour                   = 2
	DevTierMaxAutoBackups            uint64 = 1
	DevTierMaxManualBackups          uint64 = 2
	GiB                                     = 1024 * 1024 * 1024
)

// tidb admin-sql const
const (
	GetClusterTsoClase        = "SHOW MASTER STATUS;"
	SetGCEnableClause         = "SET GLOBAL TIDB_GC_ENABLE='%s';"
	SetRestrictReadonlyClause = "SET GLOBAL TIDB_RESTRICTED_READ_ONLY='%s'"
	SetSuperReadonlyClause    = "SET GLOBAL TIDB_SUPER_READ_ONLY='%s'"
	GCEnableON                = "ON"
	GCEnableOFF               = "OFF"
	PhysicalShiftBits         = 18
)

// api auth & rbac const
const (
	ApiOperatorIP  util.ContextKey = "api_operator_ip"
	ApiKeyAuthType                 = "apikey"
	GmailAuthType                  = auth0.GmailAuthType

	METADATA_GRPC_GATEWAY_TOKEN_KEY util.ContextKey = common.METADATA_GRPC_GATEWAY_TOKEN
	METADATA_GRPC_GATEWAY_VALUE                     = common.METADATA_GRPC_GATEWAY_VALUE

	CTX_IDENTITY_KEY util.ContextKey = "ctxIdentityKey"
)

const (
	// O11yAuth0Secret ... env var name for o11y auth0 M2M client secret
	O11yAuth0Secret    = "metrics-integration-svc-o11y-auth0-secret"
	O11yAuth0SecretKey = "auth0-client-secret"
)

// rbac const
var RBACPathPattern2ResourceMap = map[string]string{
	"post:/api/v1/orgs/(\\d+)/projects/(\\d+)/clusters/(\\d+)/describeSchemaTable": "orgs/{org_id}/projects/{project_id}/clusters/{cluster_id}/describeSchemaTable",
	"get:/api/v1/orgs/(\\d+)/projects/(\\d+)/clusters/(\\d+)/ticdc_clusters":       "orgs/{org_id}/projects/{project_id}/clusters/{cluster_id}/ticdc_clusters",

	// openapi
	"post:/v1beta1/backups/(\\d+)/exports":               "orgs/{org_id}/projects/{project_id}/clusters/{cluster_id}/backups/{backup_id}/exports/create",
	"post:/v1beta1/backups/(\\d+)/exports/(\\d+):cancel": "orgs/{org_id}/projects/{project_id}/clusters/{cluster_id}/backups/{backup_id}/exports/{export_id}/cancel",
	"get:/v1beta1/backups/(\\d+)/exports":                "orgs/{org_id}/projects/{project_id}/clusters/{cluster_id}/backups/exports/get",
	"get:/v1beta1/backups/(\\d+)/exports/(\\d+)":         "orgs/{org_id}/projects/{project_id}/clusters/{cluster_id}/backups/exports/get",
	"get:/v1beta1/backups/(\\d+)/exportProfile":          "orgs/{org_id}/projects/{project_id}/clusters/{cluster_id}/backups/{backup_id}/export-profile/get",
}

type MasterStatus struct {
	Position []uint64 `field:"Position"`
}
