package cluster

import (
	"github.com/pingcap-inc/clinic/pkg/db"
)

type ListClustersResp struct {
	Total int64     `json:"total"`
	Items []Cluster `json:"items"`
}

type AddClusterReq struct {
	Name         string `json:"name"`
	Description  string `json:"description"`
	CreatedAt    uint64 `json:"createAt"`
	ClusterType  string `json:"clusterType"`
	DeployType   string `json:"deployType"`
	DeployTypeV2 string `json:"deployTypeV2"`
	ClusterID    string `json:"clusterID"`
	ProjectID    string `json:"projectID"`
	Region       string `json:"region"`
	Provider     string `json:"provider"`
}

type AddClusterResp struct {
	ID string `json:"id"`
}

type UpdateClusterReq struct {
	Name        string `json:"name"`
	Description string `json:"description"`
}

type UpdateClusterStatusReq struct {
	Status string `json:"status"`
}

type Cluster struct {
	ID      string `json:"id"`
	Name    string `json:"name"`
	Version string `json:"version"`

	ClusterType    string `json:"clusterType"`
	StartTime      uint64 `json:"startTime"`
	EndTime        uint64 `json:"endTime"`
	LastestUpdated uint64 `json:"lastestUpdated"`
	Created        uint64 `json:"created"`

	// PackageCount int64 `json:"package_count"`
	TotalReport int64 `json:"totalReport"`
	TotalOncall int64 `json:"totalOncall"`

	Topology *db.Topology `json:"topology"`
	Note     string       `json:"note"`
	Status   string       `json:"status"`
	// Usage by the cluster in bytes
	DataSizeUsed  uint64 `json:"dataSizeUsed"`
	DataSizeLimit uint64 `json:"dataSizeLimit"`

	ShootName        string                         `json:"shootName"`
	DataPlaneName    string                         `json:"dataPlaneName"`
	K8sNamespace     string                         `gorm:"k8sNamespace"`
	NetworkName      string                         `gorm:"networkName"`
	Region           string                         `json:"region"`
	Provider         string                         `json:"provider"`
	DeployType       string                         `json:"deployType"`
	DeployTypeV2     string                         `json:"deployTypeV2"`
	ProjectID        string                         `json:"projectID"`
	Components       map[string]db.ClusterComponent `json:"components"`
	Branches         []string                       `json:"branches"`
	ControlPlaneArch string                         `json:"controlPlaneArch"`
	FeatureGates     ClusterFeatureGates            `json:"featureGates"`
	ParentID         string                         `json:"parentID"`     // nextgen physic cluster id
	MinRCU           int64                          `json:"minRCU"`       // nextgen min rcu
	MaxRCU           int64                          `json:"maxRCU"`       // nextgen max rcu
	KeyspaceID       uint32                         `json:"keyspaceID"`   // keyspace id for serverless cluster
	KeyspaceName     string                         `json:"keyspaceName"` // keyspace name for serverless cluster
	SpendLimit       int64                          `json:"spendLimit"`   // spend limit for serverless cluster
	RuVersion        int32                          `json:"ruVersion"`    // resource unit version from mgmt (Essential/Premium)
}

type ClusterFeatureGates struct {
	LogsEnabled              bool `json:"logsEnabled"`
	MaterializedViewEnabled  bool `json:"materializedViewEnabled"`
	SlowQueryEnabled         bool `json:"slowQueryEnabled"`
	SlowQueryVisualEnabled   bool `json:"slowQueryVisualEnabled"`
	TopSQLEnabled            bool `json:"topSQLEnabled"`
	ConProfEnabled           bool `json:"conProfEnabled"`
	BenchmarkReportEnabled   bool `json:"benchmarkReportEnabled"`
	ComparisonReportEnabled  bool `json:"comparisonReportEnabled"`
	SystemCheckReportEnabled bool `json:"systemCheckReportEnabled"`
}

type DefaultConfiguration struct {
	Items map[string][]db.DefaultConfiguration `json:"items"`
}

type ClusterMetrics struct {
	SLA           string      `json:"sla" type:"string"`
	MaxCpu        MetricValue `json:"maxCpu"`
	MaxMemory     MetricValue `json:"maxMemory"`
	MinUpTime     MetricValue `json:"minUpTime"`
	ReportsNumber int64       `json:"reports"`
	EventsNumber  int         `json:"events"`
	StorageSize   string      `json:"storageSize" type:"string"`
}

type MetricValue struct {
	Component string   `json:"component"`
	Value     uaDouble `json:"-"`
	ValueUI   string   `json:"value"`
}

type Topology struct {
	TiDB int `json:"tidb"`
	TiKV int `json:"tikv"`
	PD   int `json:"pd"`
}

type DataSummaryResponse struct {
	Total     int64       `json:"total"`
	DataInfos []*DataInfo `json:"dataInfos"`
}

type DataInfo struct {
	StartTime  int64    `json:"startTime"`
	EndTime    int64    `json:"endTime"`
	ItemID     string   `json:"itemID"`
	FileName   string   `json:"filename"`
	Collectors []string `json:"collectors"`
	HaveLog    bool     `json:"haveLog"`
	HaveMetric bool     `json:"haveMetric"`
	HaveConfig bool     `json:"haveConfig"`
}

// TODO: UI need to use dashboard response, so it do not follow hump format
type SlowQueryListRequest struct {
	ItemID    string   `json:"itemID" form:"itemID"`
	BeginTime int      `json:"begin_time" form:"begin_time"`
	EndTime   int      `json:"end_time" form:"end_time"`
	DB        []string `json:"db" form:"db"`
	Limit     int      `json:"limit" form:"limit"`
	Text      string   `json:"text" form:"text"`
	OrderBy   string   `json:"orderBy" form:"orderBy"`
	IsDesc    bool     `json:"desc" form:"desc"`

	// for showing slow queries in the statement detail page
	Plans  []string `json:"plans" form:"plans"`
	Digest string   `json:"digest" form:"digest"`

	// Fields string `json:"fields" form:"fields"` // example: "Query,Digest"
}

//type SlowQueryDetailRequest struct {
//	ItemID string `json:"itemID"`
//}

//type SlowQueryDetailResponse struct {
//	Items map[string]interface{} `json:"items"`
//}

//type SlowQueryListResponse struct {
//	Items []map[string]interface{} `json:"items"`
//}

type SlowQueryProfile map[string]any

type DataUrl struct {
	// Uid   string `json:"uid"`
	URL  string `json:"url"`
	Name string `json:"name"`
}

//	type DownloadRequest struct {
//		ItemID string `json:"itemID"`
//	}
type RawDashboard struct {
	Title string `json:"title"`
	Uid   string `json:"uid"`
	Url   string `json:"url"`
}

type FileMeta struct {
	TotalSize  string   `json:"totalSize"`
	BriefSize  string   `json:"briefSize"`
	FileName   string   `json:"filename"`
	Collectors []string `json:"collectors"`
}

type SloganResp struct {
	ID       uint   `json:"id"`
	Text     string `json:"text"`
	CreateAt int64  `json:"createAt"`
}

type SloganRespList struct {
	Total int64        `json:"total"`
	Items []SloganResp `json:"items"`
}

type SloganReq struct {
	Text string `json:"text"`
}

// SimpleClusterResponse is a simple response for cluster basic info (direct lookup by cluster id)
type SimpleClusterResponse struct {
	ClusterID    string `json:"clusterId"`
	Name         string `json:"name"`
	OrgID        uint   `json:"orgId"`
	TenantID     uint64 `json:"tenantId"`
	ProjectID    string `json:"projectId"`
	Region       string `json:"region"`
	Provider     string `json:"provider"`
	DeployType   string `json:"deployType"`
	DeployTypeV2 string `json:"deployTypeV2"`
	Status       string `json:"status"`
	Version      string `json:"version"`
	CreatedAt    int64  `json:"createdAt"`
	UpdatedAt    int64  `json:"updatedAt"`
}
