package topru

import (
	"fmt"

	"github.com/pingcap/errors"
)

type ClusterHeader struct {
	Provider   *string `header:"x-provider"`
	Region     *string `header:"x-region"`
	TenantID   *uint64 `header:"x-org-id"`
	ProjectID  *uint64 `header:"x-project-id"`
	ClusterID  *uint64 `header:"x-cluster-id"`
	DeployType string  `header:"x-deploy-type"`
}

func (h *ClusterHeader) Validate() error {
	if h.Provider == nil {
		return errors.New("provider not found")
	}
	if h.Region == nil {
		return errors.New("region not found")
	}
	if h.TenantID == nil {
		return errors.New("org id not found")
	}
	if h.ClusterID == nil {
		return errors.New("cluster id not found")
	}
	return nil
}

func (h *ClusterHeader) GetTenantID() string {
	if h == nil || h.TenantID == nil || *h.TenantID == 0 {
		return ""
	}
	return fmt.Sprintf("%v", *h.TenantID)
}

func (h *ClusterHeader) GetProjectID() string {
	if h == nil || h.ProjectID == nil || *h.ProjectID == 0 {
		return ""
	}
	return fmt.Sprintf("%v", *h.ProjectID)
}

func (h *ClusterHeader) GetClusterID() string {
	if h == nil || h.ClusterID == nil || *h.ClusterID == 0 {
		return ""
	}
	return fmt.Sprintf("%v", *h.ClusterID)
}

// ConfigResponse matches O11Y diagnosisqueryswagger definition topru.configResponse (GET /topru/config).
type ConfigResponse struct {
	Enable bool `json:"enable"`
}

// TopRUSummaryItem matches O11Y topru.TopRUSummaryItem.
type TopRUSummaryItem struct {
	DurationNs        int64     `json:"duration_ns"`
	DurationPerExecMs float64   `json:"duration_per_exec_ms"`
	ExecCount         int64     `json:"exec_count"`
	ExecCountPerSec   float64   `json:"exec_count_per_sec"`
	SQLDigest         string    `json:"sql_digest"`
	SQLText           string    `json:"sql_text"`
	TimestampSec      []int64   `json:"timestamp_sec"`
	TotalRU           float64   `json:"total_ru"`
	TotalRUByTime     []float64 `json:"total_ru_by_time"`
}

// SummaryEnvelope matches O11Y topru.summaryEnvelope.
type SummaryEnvelope struct {
	Data   []TopRUSummaryItem `json:"data"`
	Status string             `json:"status"`
}

// TopRUSummaryPlanItem matches O11Y topru.TopRUSummaryPlanItem.
type TopRUSummaryPlanItem struct {
	DurationNs        int64     `json:"duration_ns"`
	DurationPerExecMs float64   `json:"duration_per_exec_ms"`
	ExecCount         int64     `json:"exec_count"`
	ExecCountPerSec   float64   `json:"exec_count_per_sec"`
	PlanDigest        string    `json:"plan_digest"`
	PlanText          string    `json:"plan_text"`
	SQLDigest         string    `json:"sql_digest"`
	SQLText           string    `json:"sql_text"`
	TimestampSec      []int64   `json:"timestamp_sec"`
	TotalRU           float64   `json:"total_ru"`
	TotalRUByTime     []float64 `json:"total_ru_by_time"`
}

// SummaryPlanEnvelope matches O11Y topru.summaryPlanEnvelope.
type SummaryPlanEnvelope struct {
	Data   []TopRUSummaryPlanItem `json:"data"`
	Status string                 `json:"status"`
}

// UserStatsItem matches O11Y topru.UserStatsItem.
type UserStatsItem struct {
	DurationNs int64   `json:"duration_ns"`
	ExecCount  int64   `json:"exec_count"`
	TotalRU    float64 `json:"total_ru"`
	User       string  `json:"user"`
}

// UserStatsEnvelope matches O11Y topru.userStatsEnvelope.
type UserStatsEnvelope struct {
	Data   []UserStatsItem `json:"data"`
	Status string          `json:"status"`
}
