package store

import (
	"github.com/prometheus/client_golang/prometheus"
	"github.uio.no/torjus/apiary/models"
)

type MetricsCollectingStore struct {
	store           LoginAttemptStore
	attemptsCounter *prometheus.CounterVec
}

func NewMetricsCollectingStore(store LoginAttemptStore) *MetricsCollectingStore {
	mcs := &MetricsCollectingStore{store: store}

	mcs.attemptsCounter = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name:        "apiary_ssh_attempt_counter",
			Help:        "Total count of login attempts toward SSH",
			ConstLabels: prometheus.Labels{"service": "honeypot_ssh"},
		},
		[]string{"CountryCode"},
	)
	prometheus.MustRegister(mcs.attemptsCounter)

	return mcs
}

func (s *MetricsCollectingStore) AddAttempt(l *models.LoginAttempt) error {
	err := s.store.AddAttempt(l)
	s.attemptsCounter.WithLabelValues(l.Country).Inc()
	return err
}

func (s *MetricsCollectingStore) All() ([]models.LoginAttempt, error) {
	return s.store.All()
}

func (s *MetricsCollectingStore) Stats(statType LoginStats, limit int) ([]StatsResult, error) {
	return s.store.Stats(statType, limit)
}

func (s *MetricsCollectingStore) Query(query AttemptQuery) ([]models.LoginAttempt, error) {
	return s.store.Query(query)
}