{{- define "print_links" -}}
  {{- /* Panel link and GeneratorURL are both primary references */ -}}
  {{- if .PanelURL -}}
    [📊 Graph]({{ .PanelURL }})
  {{- else -}}
    {{- if and .Labels (index .Labels "panel_id") -}}
      {{- $dashboard_uid := index .Labels "dashboard_uid" -}}
      {{- if not $dashboard_uid -}}
        {{- $service := index .Labels "service" -}}
        {{- if $service -}}
          {{- $dashboard_uid = printf "%s-dashboard" $service -}}
        {{- end -}}
      {{- end -}}
      {{- if $dashboard_uid -}}
        {{- $url := printf "__GRAFANA_URL__/d/%s?orgId=__ORG_ID__&viewPanel=%s&refresh=2s" $dashboard_uid (index .Labels "panel_id") -}}
        [📊 Graph]({{ $url }})
      {{- end -}}
    {{- end -}}
  {{- end -}}
  {{- if and (eq .Status "firing") (.GeneratorURL) -}}
    {{- " " -}}[⚡ Check Alert]({{ .GeneratorURL }})
  {{- end -}}
  {{- if and (eq .Status "firing") (.SilenceURL) -}}
    {{- " " -}}[🤐 Mute]({{ .SilenceURL }})
  {{- end -}}
  {{- if .DashboardURL -}}
    {{- " " -}}[📈 Dashboard]({{ .DashboardURL }})
  {{- end -}}
{{- end -}}

{{- define "print_alert" -}}
  {{- $status := "" -}}
  {{- $severity := "" -}}
  {{- if index .Labels "severity" -}}
    {{- $severity = index .Labels "severity" -}}
  {{- end -}}

  {{- if eq .Status "resolved" -}}
    {{- $status = "🟢" -}}
  {{- else if eq $severity "disaster" -}}
    {{- $status = "🔴" -}}
  {{- else if eq $severity "critical" -}}
    {{- $status = "🟣" -}}
  {{- else if eq $severity "performance" -}}
    {{- $status = "🟡" -}}
  {{- else -}}
    {{- $status = "🟠" -}}
  {{- end -}}

  {{- $summary := index .Annotations "summary" -}}
  {{- $description := index .Annotations "description" -}} 
  {{- $alertname := index .Labels "alertname" -}}
  {{- if or (eq $alertname "DatasourceNoData") (eq $alertname "DatasourceError") -}}
    {{- $summary = $alertname -}}
    {{- $alertname = index .Labels "rulename" -}}
  {{- end -}}

  {{- $instance := index .Labels "instance" -}}
  {{- $service := "" -}}
  {{- if index .Labels "service" -}}
    {{- $service = index .Labels "service" -}}
  {{- end -}}

  {{ $status }} **{{ $instance }}**{{ "\n" }}
  {{- if $service -}}
    {{ $service }}: {{ $alertname }}{{ "\n" }}
  {{- else -}}
    {{ $alertname }}{{ "\n" }}
  {{- end -}}

  {{- if eq $severity "disaster" -}}🚨 {{ $summary }}{{ "\n" }}
  {{- else if eq $severity "critical" -}}🚨 {{ $summary }}{{ "\n" }}
  {{- else if eq $severity "performance" -}}🐌 {{ $summary }}{{ "\n" }}
  {{- else -}}⚠️ {{ $summary }}{{ "\n" }}
  {{- end -}}

  {{- if $description -}}
    {{- "\n" -}}
    {{ $description }}{{ "\n" }}
  {{- end -}}

  {{- $common_labels := coll.Dict "environment" "__ENV__" -}}
  {{- range $label, $value := $common_labels -}}
    `{{ $label }}: {{ $value }}`{{ "\n" }}
  {{- end -}}

  {{- $has_labels := false -}}
  {{- range $label, $value := .Labels -}}
    {{- if and (ne $label "alertname") (ne $label "datasource_uid") (ne $label "grafana_folder") (ne $label "job")
              (ne $label "details") (ne $label "ref_id") (ne $label "rulename") (ne $label "instance")
              (ne $label "service") (ne $label "severity") (ne $label "dashboard_uid") (ne $label "panel_id")
              (ne $label "ip_version") (ne $label "ip") (ne $label "hostname") (ne $label "role") (ne $label "team")
              (ne $label "id") (ne $label "endpoint") (ne $label "hostgroup") (ne $label "module")
              (ne $label "servername") (ne $label "type") (ne $label "vm_project_id") (ne $label "vm_account_id") -}}
      {{- $has_labels = true -}}
      {{- break -}}
    {{- end -}}
  {{- end -}}

  {{- if $has_labels -}}
    {{- range $label, $value := .Labels -}}
      {{- if and (ne $label "alertname") (ne $label "datasource_uid") (ne $label "grafana_folder") (ne $label "job")
                (ne $label "details") (ne $label "ref_id") (ne $label "rulename") (ne $label "instance")
                (ne $label "service") (ne $label "severity") (ne $label "dashboard_uid") (ne $label "panel_id")
                (ne $label "ip_version") (ne $label "ip") (ne $label "hostname") (ne $label "role") (ne $label "team")
                (ne $label "id") (ne $label "endpoint") (ne $label "hostgroup") (ne $label "module")
                (ne $label "servername") (ne $label "type") (ne $label "vm_project_id") (ne $label "vm_account_id") -}}
        `{{ $label }}: {{ $value }}`{{ "\n" }}
      {{- end -}}
    {{- end -}}
  {{- end -}}

  {{- if eq .Status "firing" -}}
    {{- "\n" -}}
    {{- template "print_links" . -}}
  {{- end -}}
{{- end -}}

{{- define "print_alerts" -}}
  {{- range $index, $alert := . -}}
    {{- if ne $index 0 -}}
      {{ "---" }}{{ "\n" }}
    {{- end -}}
    {{- template "print_alert" $alert -}}
  {{- end -}}
{{- end -}}

{{- with .Alerts -}}
  {{- if .Firing -}}
  **🔥 Firing Alerts**{{ "\n" }}
  {{ "---" }}{{ "\n" }}
  {{- template "print_alerts" .Firing -}}
  {{- end -}}

  {{- if and .Firing .Resolved -}}
    {{ "\n\n" }}
  {{- end -}}

  {{- if .Resolved -}}
  **✅ Resolved Alerts**{{ "\n" }}
  {{ "---" }}{{ "\n" }}
  {{- template "print_alerts" .Resolved -}}
  {{- end -}}
{{- end -}}