package apps

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

type Metrics struct {
	Cubebackup    prometheus.GaugeVec
	Forgejo       prometheus.GaugeVec
	ForgejoRunner prometheus.GaugeVec
	Grafana       prometheus.GaugeVec
	Minio         prometheus.GaugeVec
	Nextcloud     prometheus.GaugeVec
	Odoo          prometheus.GaugeVec
	Postgres      prometheus.GaugeVec
	Vaultwarden   prometheus.GaugeVec
	Version       prometheus.GaugeVec
}

func NewMetrics(reg prometheus.Registerer) *Metrics {
	metrics := &Metrics{
		Cubebackup:    *prometheus.NewGaugeVec(prometheus.GaugeOpts{Namespace: "app", Subsystem: "version", Name: "cubebackup", Help: ""}, []string{"version"}),
		Forgejo:       *prometheus.NewGaugeVec(prometheus.GaugeOpts{Namespace: "app", Subsystem: "version", Name: "forgejo", Help: ""}, []string{"version"}),
		ForgejoRunner: *prometheus.NewGaugeVec(prometheus.GaugeOpts{Namespace: "app", Subsystem: "version", Name: "forgejo_runner", Help: ""}, []string{"version"}),
		Grafana:       *prometheus.NewGaugeVec(prometheus.GaugeOpts{Namespace: "app", Subsystem: "version", Name: "grafana", Help: ""}, []string{"version"}),
		Minio:         *prometheus.NewGaugeVec(prometheus.GaugeOpts{Namespace: "app", Subsystem: "version", Name: "minio", Help: ""}, []string{"version"}),
		Nextcloud:     *prometheus.NewGaugeVec(prometheus.GaugeOpts{Namespace: "app", Subsystem: "version", Name: "nextcloud", Help: ""}, []string{"version"}),
		Odoo:          *prometheus.NewGaugeVec(prometheus.GaugeOpts{Namespace: "app", Subsystem: "version", Name: "odoo", Help: ""}, []string{"version", "version_date"}),
		Postgres:      *prometheus.NewGaugeVec(prometheus.GaugeOpts{Namespace: "app", Subsystem: "version", Name: "postgres", Help: ""}, []string{"version"}),
		Vaultwarden:   *prometheus.NewGaugeVec(prometheus.GaugeOpts{Namespace: "app", Subsystem: "version", Name: "vaultwarden", Help: ""}, []string{"version"}),
		Version:       *prometheus.NewGaugeVec(prometheus.GaugeOpts{Namespace: "appexporter", Name: "version", Help: "The version of this exporter"}, []string{"version"}),
	}

	reg.MustRegister(
		metrics.Cubebackup,
		metrics.Forgejo,
		metrics.ForgejoRunner,
		metrics.Grafana,
		metrics.Minio,
		metrics.Nextcloud,
		metrics.Odoo,
		metrics.Postgres,
		metrics.Vaultwarden,
		metrics.Version,
	)

	return metrics
}

// Reset all metrics
//
// Reset all except app-exporter version
//
// This should be called on every loop iteration.
func (m *Metrics) Reset() {
	m.Cubebackup.Reset()
	m.Forgejo.Reset()
	m.ForgejoRunner.Reset()
	m.Grafana.Reset()
	m.Minio.Reset()
	m.Nextcloud.Reset()
	m.Odoo.Reset()
	m.Postgres.Reset()
	m.Vaultwarden.Reset()
}