package service

import (
	"context"
	"time"

	"github.com/rs/zerolog/log"
)

type StatusConsumer interface {
	FetchEvents(ctx context.Context) error
}

type NotifierService struct {
	consumer StatusConsumer
}

func NewNotifierService(consumer StatusConsumer) *NotifierService {
	return &NotifierService{
		consumer: consumer,
	}
}

func (s *NotifierService) RunFetchEvents(ctx context.Context) error {
	backoff := 1 * time.Second

	for {
		if err := s.consumer.FetchEvents(ctx); err != nil {
			if ctx.Err() != nil {
				return ctx.Err()
			}

			log.Error().Err(err).Msgf("consume error (retrying in %d second(s))", backoff)
		}

		select {
		case <-ctx.Done():
			return ctx.Err()
		case <-time.After(backoff):
		}
	}
}
