package workers

import (
	"context"

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

// MeteringEventArchiveWorker uploads metering_events rows past the
// retention window to S3 (deterministic keys) and deletes them in the
// same pass. Separate from the raw-outbox worker: the outbox covers
// whitelist tenants on the new pipeline; this archive is the
// catch-all that keeps the table bounded regardless of flow.
//
// The gocron wrapper (wrapworker) discards worker return values, so
// failures are reported here instead: log.Error (sentry-wired) for
// infrastructure errors, MeteringArchiveTotal{failed} for row-level
// put failures.
func MeteringEventArchiveWorker() {
	store, err := metering.NewMeteringArchiveStore(context.Background())
	if err != nil {
		log.Error("metering event archive: failed to create s3 store", zap.Error(err))
		return
	}
	if err := metering.ProcessMeteringEventArchive(context.Background(), store); err != nil {
		log.Error("metering event archive: run failed", zap.Error(err))
	}
}
