package task

import (
	"context"

	"github.com/hibiken/asynq"
	"go.uber.org/zap"

	pollingSvc "github.com/break/junhong_cmp_fiber/internal/service/polling"
)

// DataCleanupHandler 数据清理任务处理器
type DataCleanupHandler struct {
	cleanupService *pollingSvc.CleanupService
	logger         *zap.Logger
}

// NewDataCleanupHandler 创建数据清理处理器
func NewDataCleanupHandler(cleanupService *pollingSvc.CleanupService, logger *zap.Logger) *DataCleanupHandler {
	return &DataCleanupHandler{
		cleanupService: cleanupService,
		logger:         logger,
	}
}

// HandleDataCleanup 处理数据清理任务
// 由 Asynq Scheduler 每天凌晨 2 点触发，执行定期数据清理
func (h *DataCleanupHandler) HandleDataCleanup(ctx context.Context, _ *asynq.Task) error {
	h.logger.Info("开始执行定时数据清理")

	if err := h.cleanupService.RunScheduledCleanup(ctx); err != nil {
		h.logger.Error("定时数据清理失败", zap.Error(err))
		return err
	}

	return nil
}
