package com.sundot.airport.exam.listener;

import com.sundot.airport.exam.event.ClockInEvent;
import com.sundot.airport.exam.service.IDailyTaskGenerationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * <b>功能名：</b>ClockInEventListener<br>
 * <b>说明：</b> 考勤打卡事件监听器 <br>
 * <b>著作权：</b> Copyright (C) 2025 SUNDOT CORPORATION<br>
 * <b>修改履历：
 *
 * @author Simon Lin
 */
@Component
public class ClockInEventListener {

    private static final Logger log = LoggerFactory.getLogger(ClockInEventListener.class);

    @Autowired
    private IDailyTaskGenerationService dailyTaskGenerationService;

    /**
     * 监听打卡事件，异步生成每日答题任务
     *
     * @param event 打卡事件
     */
    @Async
    @EventListener
    public void handleClockInEvent(ClockInEvent event) {
        log.info("收到打卡事件 - userId: {}, deptId: {}",
                event.getUserId(), event.getDeptId());

        try {
            dailyTaskGenerationService.handleClockInEvent(
                    event.getUserId(),
                    event.getDeptId()
            );
        } catch (Exception e) {
            log.error("处理打卡事件失败 - userId: {}", event.getUserId(), e);
        }
    }
}
