{{- define "print_links" -}}
  {{- /* Panel link and GeneratorURL are both primary references */ -}}
  {{- if .PanelURL -}}
    <{{ .PanelURL }}|📊 Graph>
  {{- else -}}
    {{- /* Use dashboard_uid and panel_id from labels with fallback to service-based dashboard_uid */ -}}
    {{- 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 and $dashboard_uid -}}
        {{- $url := printf "__GRAFANA_URL__/d/%s?orgId=__ORG_ID__&viewPanel=%s&refresh=2s" $dashboard_uid (index .Labels "panel_id") -}}
        <{{ $url }}|📊 Graph>
      {{- end -}}
    {{- end -}}
  {{- end -}}
  {{- if and (eq .Status "firing") (.GeneratorURL) -}}
    {{- " " -}}<{{ .GeneratorURL }}|⚡ Check Alert>
  {{- end -}}
  {{- if and (eq .Status "firing") (.SilenceURL) -}}
    {{- " " -}}<{{ .SilenceURL }}|🤐 Mute>
  {{- end -}}
  {{- if .DashboardURL -}}
    {{- " " -}}<{{ .DashboardURL }}|dashboard>
  {{- 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 -}}
  {{ "\n" }}
  {{- 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 -}}

  {{- $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") (ne $label "environment") (ne $label "rack") (ne $label "server_type") -}}
      {{- $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") (ne $label "environment") (ne $label "rack") (ne $label "server_type") -}}
        {{ $label }}: {{ $value }}{{ "\n" }}
      {{- end -}}
    {{- end -}}
  {{- end -}}
  
  {{- if eq .Status "firing" -}}
    {{- if $has_labels -}}{{- "\n" -}}{{- end -}}
    {{- 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 -}}
