package cn.novalon.gym.manage.groupcourse.event;

import cn.novalon.gym.manage.groupcourse.event.BookingReminderEvent.ReminderType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * 预约提醒事件监听器
 * 
 * 监听预约相关事件，处理提醒业务
 * 
 * @author 张翔
 * @date 2026-06-01
 */
@Component
public class BookingReminderEventListener {

    private static final Logger logger = LoggerFactory.getLogger(BookingReminderEventListener.class);

    /**
     * 处理预约提醒事件
     * 
     * @param event 预约提醒事件
     */
    @EventListener
    public void handleBookingReminderEvent(BookingReminderEvent event) {
        logger.info("收到预约提醒事件：type={}, bookingId={}, memberId={}", 
                event.getType(), event.getBookingId(), event.getMemberId());
        
        try {
            processReminderEvent(event);
        } catch (Exception e) {
            logger.error("处理预约提醒事件失败：bookingId={}, error={}", 
                    event.getBookingId(), e.getMessage(), e);
        }
    }

    /**
     * 处理提醒事件
     * 
     * 根据事件类型执行不同的提醒逻辑
     */
    private void processReminderEvent(BookingReminderEvent event) {
        switch (event.getType()) {
            case BOOKING_SUCCESS:
                handleBookingSuccess(event);
                break;
            case COURSE_REMINDER:
                handleCourseReminder(event);
                break;
            case BOOKING_CANCEL:
                handleBookingCancel(event);
                break;
            default:
                logger.warn("未知的提醒事件类型：{}", event.getType());
        }
        
        logger.info("预约提醒事件处理完成：bookingId={}", event.getBookingId());
    }

    /**
     * 处理预约成功事件
     */
    private void handleBookingSuccess(BookingReminderEvent event) {
        logger.info("处理预约成功提醒：会员ID={}, 课程={}, 时间={}", 
                event.getMemberId(), event.getCourseName(), event.getCourseTime());
        
        // 实际业务中会调用通知服务发送短信、APP推送等
        // sendNotification(event.getMemberId(), "预约成功", 
        //     "您已成功预约课程：" + event.getCourseName() + "，时间：" + event.getCourseTime());
    }

    /**
     * 处理课程即将开始提醒事件
     */
    private void handleCourseReminder(BookingReminderEvent event) {
        logger.info("处理课程提醒：会员ID={}, 课程={}, 时间={}", 
                event.getMemberId(), event.getCourseName(), event.getCourseTime());
        
        // 实际业务中会调用通知服务发送课程开始前提醒
        // sendNotification(event.getMemberId(), "课程提醒", 
        //     "您预约的课程" + event.getCourseName() + "即将开始，时间：" + event.getCourseTime());
    }

    /**
     * 处理预约取消事件
     */
    private void handleBookingCancel(BookingReminderEvent event) {
        logger.info("处理预约取消提醒：会员ID={}, 课程={}", 
                event.getMemberId(), event.getCourseName());
        
        // 实际业务中会调用通知服务发送预约取消通知
        // sendNotification(event.getMemberId(), "预约取消", 
        //     "您已取消课程：" + event.getCourseName() + "的预约");
    }
}