package task

import (
	"context"

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

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

// AlertCheckHandler 告警检查任务处理器
type AlertCheckHandler struct {
	alertService *pollingSvc.AlertService
	logger       *zap.Logger
}

// NewAlertCheckHandler 创建告警检查处理器
func NewAlertCheckHandler(alertService *pollingSvc.AlertService, logger *zap.Logger) *AlertCheckHandler {
	return &AlertCheckHandler{
		alertService: alertService,
		logger:       logger,
	}
}

// HandleAlertCheck 处理告警检查任务
// 由 Asynq Scheduler 每分钟触发，检查所有告警规则
func (h *AlertCheckHandler) HandleAlertCheck(ctx context.Context, _ *asynq.Task) error {
	if err := h.alertService.CheckAlerts(ctx); err != nil {
		h.logger.Error("告警检查失败", zap.Error(err))
		return err
	}

	return nil
}
