package tidbcloud

import (
	"encoding/json"

	"github.com/pingcap/crm-bot/pkg/util"
)

type RespCloudTenant struct {
	Total int           `json:"total"`
	Items []CloudTenant `json:"items"`
}

type CloudTenant struct {
	ID                        string `json:"id" gorm:"primaryKey"`
	Name                      string `json:"name"`
	Alias                     string `json:"alias"`
	Timezone                  int    `json:"timezone"`
	Email                     string `json:"email"`
	Status                    string `json:"status"`
	Plan                      string `json:"plan"`
	Discount                  int    `json:"discount"`
	DiscountDescription       string `json:"discount_description"`
	Country                   string `json:"country"`
	HasDiscount               bool   `json:"has_discount"`
	CreatedAt                 int    `json:"created_at"`
	Kind                      string `json:"kind"`
	AccountManager            string `json:"account_manager"`
	ThirdPartyAccountProvider string `json:"third_party_account_provider"`
	ThirdPartyAccountCode     string `json:"third_party_account_code"`
}

type RespCloudTenantCluster struct {
	Total int                  `json:"total"`
	Items []CloudTenantCluster `json:"items"`
}

type CloudTenantCluster struct {
	TenantId    string                                  `json:"tenant_id" gorm:"primaryKey"`
	ID          string                                  `json:"id" gorm:"primaryKey"`
	Name        string                                  `json:"name"`
	Provider    string                                  `json:"provider"`
	Region      string                                  `json:"region"`
	Profile     string                                  `json:"profile"`
	Version     string                                  `json:"version"`
	CreateTime  int64                                   `json:"create_time"`
	Status      string                                  `json:"status"`
	ProjectID   string                                  `json:"project_id"`
	ProjectName string                                  `json:"project_name"`
	IsDevTier   bool                                    `json:"is_dev_tier"`
	Components  *map[string]CloudTenantClusterComponent `json:"tenant_components" gorm:"-:all"`
}

type CloudTenantNodeProfile struct {
	Compute struct {
		CPU      int `json:"cpu"`
		MemoryMi int `json:"memory_mi"`
	} `json:"compute"`
	StorageGi     int    `json:"storage_gi"`
	NodeProfileID string `json:"node_profile_id"`
	Name          string `json:"name"`
}

func (profile *CloudTenantNodeProfile) Json2String() (string, error) {
	bs, err := json.Marshal(profile)
	return util.ByteSliceToString(bs), err
}

type CloudTenantClusterComponent struct {
	ClusterId     string                 `json:"cluster_id" gorm:"primaryKey"`
	ComponentName string                 `json:"component_name" gorm:"primaryKey"`
	Replicas      int                    `json:"replicas"`
	NodeInfo      string                 `json:"-"`
	NodeProfile   CloudTenantNodeProfile `json:"node_profile" gorm:"-:all"`
	MachineType   string                 `json:"machineType"`
}
