package utils

import (
	prometheusv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
	"k8s.io/apimachinery/pkg/util/intstr"
)

type RuleOption func(*prometheusv1.PrometheusRuleSpec)

func OverlayRule(rule *prometheusv1.PrometheusRuleSpec) {
	// updated for default rule template
	rule.Groups[0] = prometheusv1.RuleGroup{
		Name: "resources",
		Rules: []prometheusv1.Rule{
			{
				Alert: "OverlayRuleTest",
				Expr:  intstr.FromString("minute() < 0"), // a promql that never trigger alerts
				For:   "10m",
				Labels: map[string]string{
					"severity":      "major",
					"visibility":    "external",
					"ruleID":        "5",
					"status":        "active",
					"canEdit":       "all",
					"monitorTarget": "Total TiDB node CPU utilizatio ",
					"operator":      " is greater than {threshold}",
					"message":       `Total TiDB node CPU utilization of cluster TiDBClusterName in project TiDBProjectName has exceeded exceeded {thresholdPercentage}% for {triggerInterval} minutes. Consider increasing the node number or node size for TiDB to reduce the CPU usage percentage of the current workload. To monitor node CPU utilization, see <a href="https://TiDBCloudDomainName/console/clusters/{{ $labels.tidb_cluster_id }}/monitoring/metrics">Monitoring page</a> topic.`,
					"description":   `Total TiDB node CPU utilization exceeded {threshold} for {triggerInterval} minutes`,
				},
				Annotations: map[string]string{
					"message":     `Total TiDB node CPU utilization of cluster TiDBClusterName in project TiDBProjectName has exceeded exceeded {thresholdPercentage}% for {triggerInterval} minutes. Consider increasing the node number or node size for TiDB to reduce the CPU usage percentage of the current workload. To monitor node CPU utilization, see <a href="https://TiDBCloudDomainName/console/clusters/{{ $labels.tidb_cluster_id }}/monitoring/metrics">Monitoring page</a> topic.`,
					"description": `Total TiDB node CPU utilization exceeded {threshold} for {triggerInterval} minutes`,
				},
			},
		},
	}
}

func MessageChange(rule *prometheusv1.PrometheusRuleSpec) {
	// updated for default rule template
	rule.Groups[0].Rules[0].Labels["message"] = "Overlay Test {threshold}"
	rule.Groups[0].Rules[0].Annotations["message"] = "Overlay Test {threshold}"
}

func DefaultRuleTemplate() prometheusv1.PrometheusRuleSpec {
	return prometheusv1.PrometheusRuleSpec{
		Groups: []prometheusv1.RuleGroup{
			0: {
				Name: "test",
				Rules: []prometheusv1.Rule{
					{
						Alert: "HighRequestLatency",
						Expr:  intstr.FromString("request_latency_seconds:mean5m{job=\"myjob\"} > 0.5"),
						For:   "10m",
						Labels: map[string]string{
							"severity":   "critical",
							"visibility": "external",
							"ruleID":     "5",
							"status":     "active",
						},
						Annotations: map[string]string{
							"summary": "High request latency",
						},
					},
				},
			},
			1: {
				Name: "test",
				Rules: []prometheusv1.Rule{
					{
						Alert: "HighRequestLatency2",
						Expr:  intstr.FromString("request_latency_seconds:mean5m{job=\"myjob\"} > 0.5"),
						For:   "10m",
						Labels: map[string]string{
							"severity":   "critical",
							"visibility": "external",
							"ruleID":     "5",
							"status":     "active",
						},
						Annotations: map[string]string{
							"summary": "High request latency",
						},
					},
				},
			},
		},
	}
}
