package topsql

import (
	"fmt"
	"time"

	"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.ProjectID == nil {
	// 	return errors.New("project 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)
}

type InstanceQuery struct {
	StartSecs *float64 `form:"start"`
	EndSecs   *float64 `form:"end"`
}

func (q *InstanceQuery) Validate() error {
	if q.StartSecs == nil {
		return errors.New("start not found")
	}
	if q.EndSecs == nil {
		return errors.New("end not found")
	}
	return nil
}

type SummaryQuery struct {
	StartSecs    *float64       `form:"start"`
	EndSecs      *float64       `form:"end"`
	Window       *time.Duration `form:"window"`
	Top          *int           `form:"top"`
	Instance     *string        `form:"instance"`
	InstanceType *string        `form:"instance_type"`
}

func (q *SummaryQuery) Validate() error {
	if q.StartSecs == nil {
		return errors.New("start not found")
	}
	if q.EndSecs == nil {
		return errors.New("end not found")
	}
	if q.Window == nil {
		return errors.New("window not found")
	}
	if q.Top == nil {
		return errors.New("top not found")
	}
	if q.Instance == nil {
		return errors.New("instance not found")
	}
	if q.InstanceType == nil {
		return errors.New("instance_type not found")
	}
	return nil
}
