package com.seer.teach.teacher.stateMachine.action.summary;

import com.seer.teach.common.enums.teacher.LessonStateEnum;
import com.seer.teach.common.enums.teacher.LessonStateEvent;
import com.seer.teach.teacher.common.dto.LessonContextDTO;
import com.seer.teach.teacher.common.dto.TeachDTO;
import com.seer.teach.teacher.service.LessonsService;
import com.seer.teach.teacher.service.impl.LessonStateMachineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.statemachine.StateContext;
import org.springframework.statemachine.action.Action;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SummaryFinishTimeOutAction implements Action<LessonStateEnum, LessonStateEvent> {

    @Lazy
    @Autowired
    private LessonsService lessonsService;

    @Lazy
    @Autowired
    private LessonStateMachineService lessonStateMachineService;

    /**
     * 执行状态机动作，处理课程总结阶段超时逻辑
     *
     * @param stateContext 状态机上下文对象，包含当前状态、事件和扩展状态信息
     *                     从中可以获取课程上下文和用户ID等信息
     */
    @Override
    public void execute(StateContext<LessonStateEnum, LessonStateEvent> stateContext) {
        // 提取课程上下文和用户ID
        LessonContextDTO ctx = stateContext.getExtendedState().get("ctx", LessonContextDTO.class);
        Integer userId = Integer.valueOf(stateContext.getStateMachine().getId());
        TeachDTO teachDTO = ctx.getTeachDTO();
        lessonsService.startNextLesson(teachDTO, userId);
    }
}