package workers

import (
	"context"

	"github.com/pingcap/dbaas/billing2/model/billing"
	"github.com/pingcap/dbaas/billing2/utils/log"
	"go.uber.org/zap"
)

// PricingEventArchiveWorker uploads pricing_events rows past the
// retention window to S3 (deterministic keys) and deletes them in the
// same pass. Ships always-on: the retention window itself is the only
// control (a very large retention effectively disables archiving), and
// the index migration lands in the same deploy.
//
// The gocron wrapper (wrapworker) discards worker return values, so
// failures are reported here instead: log.Error (sentry-wired) for
// infrastructure errors, PricingArchiveTotal{failed} for row-level
// put failures.
func PricingEventArchiveWorker() {
	store, err := billing.NewPricingArchiveStore(context.Background())
	if err != nil {
		log.Error("pricing event archive: failed to create s3 store", zap.Error(err))
		return
	}
	if err := billing.ProcessPricingEventArchive(context.Background(), store); err != nil {
		log.Error("pricing event archive: run failed", zap.Error(err))
	}
}
