package jnpf.cultivate.v2.controller.web.aihelper;

import cn.hutool.core.collection.CollUtil;
import jnpf.base.ActionResult;
import jnpf.cultivate.v2.service.V2CultivateCourseAppService;
import jnpf.cultivate.v2.service.V2CultivateCourseService;
import jnpf.cultivate.v2.service.V2CultivateExamStatisticsService;
import jnpf.cultivate.v2.service.V2CultivatePositionService;
import jnpf.model.cultivate.v2.course.vo.AiHelperCourseStatisticsVo;
import jnpf.model.cultivate.v2.course.vo.UserLearningStatusVo;
import jnpf.model.cultivate.v2.course.vo.app.LastStudyCourseVo;
import jnpf.model.cultivate.v2.exam.vo.AiHelperExamStatisticsVo;
import jnpf.model.cultivate.v2.position.vo.*;
import jnpf.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
/**
 * ai助手-培训接口
 *
 * @author xgl
 * @date 2026-01-27 14:04:01
 */
@Slf4j
@RestController
@RequestMapping("/v2/ai-helper")
public class V2CultivateAiHelperController {


    @Autowired
    private V2CultivateCourseAppService v2CultivateCourseAppService;

    @Autowired
    private V2CultivatePositionService v2CultivatePositionService;

    @Autowired
    private V2CultivateExamStatisticsService v2CultivateExamStatisticsService;

    @Autowired
    private V2CultivateCourseService v2CultivateCourseService;

    /**
     * 最后一次学习完成的课程
     */
    @GetMapping("/course/query-last-study-course")
    public ActionResult<LastStudyCourseVo> queryLastStudyCourse() {
        LastStudyCourseVo vo = v2CultivateCourseAppService.queryLastStudyCourse();
        if (StringUtil.isEmpty(vo.getCourseId())) {
            return ActionResult.success("成功", null);
        }
        if (vo.getState().equals(1)) {
            return ActionResult.success("成功", null);
        }
        return ActionResult.success("成功", vo);
    }

    /**
     * 获取用户本岗位学习数据
     *
     * @return
     */
    @GetMapping("/self-position/position-study-detail")
    public ActionResult<AppCultivatePositionDetailVo> positionStudyDetail() {
        AppCultivatePositionDetailVo appCultivatePositionDetailVo = v2CultivatePositionService.positionStudyDetail(new V2CultivatePositionDetailForApp());
        if (CollUtil.isNotEmpty(appCultivatePositionDetailVo.getFtbCultivatePositionCourseList())) {
            List<AppCultivatePositionCourseVo> ftbCultivatePositionCourseList = new ArrayList<>();
            for (AppCultivatePositionCourseVo item : appCultivatePositionDetailVo.getFtbCultivatePositionCourseList()) {
                if (item.getLearnState().equals(1)) {
                    ftbCultivatePositionCourseList.add(item);
                }
            }
            appCultivatePositionDetailVo.setFtbCultivatePositionCourseList(ftbCultivatePositionCourseList);
        }
        if (CollUtil.isNotEmpty(appCultivatePositionDetailVo.getExam())) {
            List<AppCultivatePositionCourseExamVo> exam = new ArrayList<>();
            for (AppCultivatePositionCourseExamVo item : appCultivatePositionDetailVo.getExam()) {
                if (item.getLearnState().equals(1)) {
                    exam.add(item);
                }
            }
            appCultivatePositionDetailVo.setExam(exam);
        }
        if (CollUtil.isNotEmpty(appCultivatePositionDetailVo.getIdentity())) {
            List<AppCultivatePositionCourseIdentityVo> identity = new ArrayList<>();
            for (AppCultivatePositionCourseIdentityVo item : appCultivatePositionDetailVo.getIdentity()) {
                if (item.getIsCanIdentification().equals(1) && item.getIdentificationStatus().equals(1)) {
                    identity.add(item);
                }
            }
            appCultivatePositionDetailVo.setIdentity(identity);
        }
        if (CollUtil.isNotEmpty(appCultivatePositionDetailVo.getPracticeList())) {
            List<AppCultivatePositionCoursePracticeVo> practiceList = new ArrayList<>();
            for (AppCultivatePositionCoursePracticeVo item : appCultivatePositionDetailVo.getPracticeList()) {
                if (item.getLearnState().equals(1)) {
                    practiceList.add(item);
                }
            }
            appCultivatePositionDetailVo.setPracticeList(practiceList);
        }
        return ActionResult.success("成功", appCultivatePositionDetailVo);

    }

    /**
     * 根据考试ID统计考试数据
     *
     * @param examId 考试ID
     * @return 考试统计结果
     */
    @GetMapping("/exam/statistics")
    public ActionResult<AiHelperExamStatisticsVo> getExamStatistics(@RequestParam(value = "examId") String examId) {
        AiHelperExamStatisticsVo statistics = v2CultivateExamStatisticsService.getExamStatistics(examId);
        return ActionResult.success("成功", statistics);
    }

    /**
     * 根据课程ID统计课程学习数据
     *
     * @param courseId 课程ID
     * @return 课程学习统计结果
     */
    @GetMapping("/course/statistics")
    public ActionResult<AiHelperCourseStatisticsVo> getCourseStatistics(@RequestParam(value = "courseId") String courseId) {
        AiHelperCourseStatisticsVo statistics = v2CultivateCourseService.getCourseStatistics(courseId);
        return ActionResult.success("成功", statistics);
    }

    /**
     * 查询指定用户的学习情况
     *
     * @param userId 用户ID
     * @return 用户学习情况（包含已完成的课程、考试、鉴定列表，已去重）
     */
    @GetMapping("/user/learning-status")
    public ActionResult<UserLearningStatusVo> getUserLearningStatus(@RequestParam(value = "userId") String userId) {
        UserLearningStatusVo learningStatus = v2CultivateCourseService.getUserLearningStatus(userId);
        return ActionResult.success("成功", learningStatus);
    }


}
