package task

import (
	"context"

	"git.huangwc.com/pig/pig-farm-controller/internal/domain/alarm"
	"git.huangwc.com/pig/pig-farm-controller/internal/domain/plan"
	"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
	"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
)

type RefreshNotificationTask struct {
	ctx     context.Context
	taskLog *models.TaskExecutionLog

	alarmService alarm.AlarmService
}

func NewRefreshNotificationTask(ctx context.Context, taskLog *models.TaskExecutionLog, alarmService alarm.AlarmService) plan.Task {
	return &RefreshNotificationTask{
		ctx:          ctx,
		taskLog:      taskLog,
		alarmService: alarmService,
	}
}

func (r *RefreshNotificationTask) Execute(ctx context.Context) error {
	taskCtx, logger := logs.Trace(ctx, r.ctx, "Execute")
	logger.Infof("开始执行刷新通知任务, 任务ID: %d", r.taskLog.TaskID)

	_, err := r.alarmService.ClearExpiredIgnoredAlarms(taskCtx)
	if err != nil {
		return err
	}

	logger.Infof("刷新通知任务执行完成, 任务ID: %d", r.taskLog.TaskID)
	return nil
}

func (r *RefreshNotificationTask) OnFailure(ctx context.Context, executeErr error) {
	logger := logs.TraceLogger(ctx, r.ctx, "OnFailure")
	logger.Errorf("刷新通知任务执行失败, 任务ID: %d, 错误: %v", r.taskLog.TaskID, executeErr)
}

func (r *RefreshNotificationTask) ResolveDeviceIDs(ctx context.Context) ([]uint32, error) {
	// 刷新通知任务不与具体设备绑定
	return []uint32{}, nil
}
