// Package metrics defines Prometheus metrics for alertstore's own health.
// The system that pages people must itself be observable.
package metrics

import (
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"
)

var (
	WebhookRequests = promauto.NewCounterVec(prometheus.CounterOpts{
		Name: "alertstore_webhook_requests_total",
		Help: "Incoming webhook requests by source and outcome.",
	}, []string{"source", "outcome"})

	// WebhookAuthorized breaks authorized requests down by accepted token name,
	// so per-sender usage is visible and rotation is provably done once the
	// retired token's series goes flat.
	WebhookAuthorized = promauto.NewCounterVec(prometheus.CounterOpts{
		Name: "alertstore_webhook_authorized_total",
		Help: "Authorized webhook requests by source and accepted token name.",
	}, []string{"source", "token"})

	EventsIngested = promauto.NewCounterVec(prometheus.CounterOpts{
		Name: "alertstore_events_ingested_total",
		Help: "Alertmanager alert instances ingested, by status and final classification.",
	}, []string{"status", "classification"})

	EventsUnclassified = promauto.NewCounter(prometheus.CounterOpts{
		Name: "alertstore_events_unclassified_total",
		Help: "Events missing the classification label (defaulted to logging). Should stay at zero; a rise means a misconfigured alert rule.",
	})

	EventsDeduplicated = promauto.NewCounter(prometheus.CounterOpts{
		Name: "alertstore_events_deduplicated_total",
		Help: "Duplicate event inserts skipped (webhook redeliveries within the dedup window).",
	})

	EventsMuted = promauto.NewCounter(prometheus.CounterOpts{
		Name: "alertstore_events_muted_total",
		Help: "Events suppressed by mute rules.",
	})

	AlertsCreated = promauto.NewCounterVec(prometheus.CounterOpts{
		Name: "alertstore_alerts_created_total",
		Help: "New alerts created, by classification.",
	}, []string{"classification"})

	OncallLookups = promauto.NewCounterVec(prometheus.CounterOpts{
		Name: "alertstore_oncall_lookups_total",
		Help: "On-call owner resolutions for new alerts, by outcome (primary/backup/manager/no_team/schedule_gap/lookup_error/disabled). Sustained manager outcomes mean a rotation has a hole; the rest mean alerts are landing unowned.",
	}, []string{"outcome"})

	AlertsDeduplicated = promauto.NewCounter(prometheus.CounterOpts{
		Name: "alertstore_alerts_deduplicated_total",
		Help: "Firing events appended to an existing open alert.",
	})

	DeliveryTasks = promauto.NewCounterVec(prometheus.CounterOpts{
		Name: "alertstore_delivery_tasks_total",
		Help: "Delivery task executions, by kind and result (success/retry/failed).",
	}, []string{"kind", "result"})

	DeliveryPending = promauto.NewGauge(prometheus.GaugeOpts{
		Name: "alertstore_delivery_tasks_pending",
		Help: "Delivery tasks currently pending. Sustained growth means IM delivery is stuck.",
	})

	// JiraIssues watches the transitional Jira mirror, and specifically the
	// canary split: during cutover exactly one of jiralert and alertstore
	// creates each issue, and this counter is where a misconfigured allow-list
	// shows up as issues appearing for receivers jiralert still owns.
	JiraIssues = promauto.NewCounterVec(prometheus.CounterOpts{
		Name: "alertstore_jira_issues_total",
		Help: "Jira compatibility issues alertstore attempted to create, by outcome (created/error).",
	}, []string{"outcome"})

	// NoticesDelivered counts customer-facing notifications mirrored into
	// internal channels. Compared against alertstore_events_ingested_total for
	// the same feed, a gap means groups asked for notices and are not getting
	// them; unlike an alert, nobody is waiting on one, so nothing else would say so.
	NoticesDelivered = promauto.NewCounterVec(prometheus.CounterOpts{
		Name: "alertstore_notices_delivered_total",
		Help: "Customer-facing notifications delivered to subscribed channels, by status.",
	}, []string{"status"})

	IMCommands = promauto.NewCounterVec(prometheus.CounterOpts{
		Name: "alertstore_im_commands_total",
		Help: "Bot commands received in alert topics, by command.",
	}, []string{"command"})
)
