package structs

import "encoding/json"

// Notification is the message we receive from Alertmanager
type Notification struct {
	Version           string            `json:"version"`
	GroupKey          string            `json:"groupKey"`
	Status            string            `json:"status"`
	Receiver          string            `json:"receiver"`
	GroupLabels       map[string]string `json:"groupLabels"`
	CommonLabels      map[string]string `json:"commonLabels"`
	CommonAnnotations map[string]string `json:"commonAnnotations"`
	ExternalURL       string            `json:"externalURL"`
	Alerts            []Alert           `json:"alerts"`
}

// Alert is a single alert.
type Alert struct {
	Status       string            `json:"status"`
	Labels       map[string]string `json:"labels"`
	Annotations  map[string]string `json:"annotations"`
	StartsAt     string            `json:"startsAt,omitempty"`
	EndsAt       string            `json:"endsAt,omitempty"`
	GeneratorURL string            `json:"generatorURL"`
}

type AlertState string

const (
	AlertStateClosed AlertState = "closed"
	AlertStateActive AlertState = "active"
	AlertStateAll    AlertState = "all"
)

type Severity string

const (
	AlertSeverityCritical Severity = "critical"
	AlertSeverityMajor    Severity = "major"
	AlertSeverityWarning  Severity = "warning"
)

// MarshalJSON Map AlertSeverityMajor and AlertSeverityWarning to "warning" in json.
func (s Severity) MarshalJSON() ([]byte, error) {
	if s == AlertSeverityMajor || s == AlertSeverityWarning {
		return []byte(`"warning"`), nil
	}
	return json.Marshal(string(s))
}

type UserAlertInfo struct {
	// alert name, the description label in alert rule.
	AlertName string `json:"alertName"`

	// alert message, the message label in alert rule.
	Message string `json:"message"`

	// duration time in seconds, e.g. 1h2m0s
	Duration string `json:"duration"`

	ActiveAt int64 `json:"activeAt"`

	Value string `json:"value"`

	State AlertState `json:"status,omitempty"`

	Severity Severity `json:"severity"`

	// alert rule name, the alert name in alert rule.
	RuleName string `json:"ruleName"`

	JobID string `json:"jobID,omitempty"`

	Changefeed string `json:"changefeed,omitempty"`
}
