package app

import (
	"context"
	"log/slog"
	"time"

	"hyapp/pkg/logx"
)

func (a *App) runActivityTemplateStatsOutboxWorker(ctx context.Context) {
	options := a.activityTemplateStatsWorkerOptions
	ticker := time.NewTicker(options.PollInterval)
	defer ticker.Stop()
	for {
		claimed, _, err := a.activityTemplate.ProcessStatsOutboxBatch(ctx, a.activityTemplateProducer,
			a.activityTemplateOutboxTopic, a.workerNodeID+":activity-template-stats", options.BatchSize, options.LockTTL)
		if err != nil && ctx.Err() == nil {
			logx.Error(ctx, "activity_template_stats_outbox_publish_failed", err, slog.String("node_id", a.workerNodeID))
		}
		if claimed >= options.BatchSize {
			continue
		}
		select {
		case <-ctx.Done():
			return
		case <-ticker.C:
		}
	}
}
