package fetcher

import (
	"log/slog"
	"time"

	"github.com/sentinela-go/internal/db"
)

func StartScheduler(database *db.DB, interval time.Duration, stop <-chan struct{}) {
	ticker := time.NewTicker(interval)
	defer ticker.Stop()

	for {
		select {
		case <-ticker.C:
			slog.Info("scheduled sync starting")
			if err := FetchAllBCB(database); err != nil {
				slog.Error("scheduled BCB sync failed", "error", err)
			}
			if err := FetchAllCVM(database); err != nil {
				slog.Error("scheduled CVM sync failed", "error", err)
			}
		case <-stop:
			return
		}
	}
}
