package repository

import (
	"context"
)

type CBType int

const (
	CBTypeCluster CBType = iota
	CBTypeBranch
)

const (
	AnnoKeyPrefix        = "free-tier.tidbcloud.com"
	AnnoTidbPoolSelector = AnnoKeyPrefix + "/tidb-pool-selector"
)

type ClusterBranchRepo interface {
	GetClusterBranchByPrimaryID(ctx context.Context, primaryID string) (*ClusterBranch, error)
	GetClusterBranchByKeyspaceID(ctx context.Context, keyspaceID string) (*ClusterBranch, error)
}

type RcuMetaRepo interface {
	ListEssentialPlanClusters(ctx context.Context) ([]*ClusterBranch, error)
}

type ClusterBranch struct {
	// type indicates the result is a cluster or a branch. 0: cluster, 1: branch.
	Type CBType `json:"type"`
	// primary_id is the primary id of cluster or branch.
	// for example, cluster: 10001, branch: bran-123.
	PrimaryID string `json:"primary_id"`
	// name is the name of branch, for cluster it's is empty.
	Name string `json:"name"`
	// cluster_id is the cluster id related to the cluster or branch.
	// for cluster, it's the primary id of cluster.
	// for branch, it's the main branch id of branch.
	ClusterID uint64 `json:"cluster_id"`
	// tenant_id is the tenant id of the resource.
	TenantID uint64 `json:"tenant_id"`
	// project_id is the project id of the resource.
	ProjectID uint64 `json:"project_id"`
	// keyspace_id is the keyspace id of the resource.
	KeyspaceID uint32 `json:"keyspace_id"`
	// keyspace_token is the keyspace token/name of the resource.
	KeyspaceToken string `json:"keyspace_token,omitempty"`
	// encrypt_keyspace indicates whether the resource has enabled "Enhanced Encryption at Rest"
	EncryptKeyspace bool `json:"encrypt_keyspace"`
	// ru_version controls whether RU metering should use v1 or v2 metrics.
	RUVersion string `json:"ru_version,omitempty"`
	// rcu config
	MaxRcu int64 `json:"max_rcu"`
	MinRcu int64 `json:"min_rcu"`
}

func (cb *ClusterBranch) IsEssentialPlan() bool {
	return cb.MaxRcu > 0 || cb.MinRcu > 0
}

func (cb *ClusterBranch) IsEncryptKeyspace() bool {
	return cb.EncryptKeyspace
}

func (cb *ClusterBranch) UseRUV2() bool {
	return cb.RUVersion == "2"
}
