package jnpf.cultivate.controller.web.aisupport;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.base.ActionResult;
import jnpf.cultivate.service.FtbCultivateCourseChapterService;
import jnpf.cultivate.service.FtbCultivateCourseService;
import jnpf.model.cultivate.po.course.FtbCultivateCourse;
import jnpf.model.cultivate.po.course.FtbCultivateCourseChapter;
import jnpf.model.cultivate.vo.course.web.CultivateCourseAiVo;
import jnpf.util.StringUtil;
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.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * web课程AI支持模块
 *
 * @author xgl
 */
@RestController
@RequestMapping("/cultivate")
public class FtbCultivateAiSupportController {

    @Autowired
    private FtbCultivateCourseService ftbCultivateCourseService;

    @Autowired
    private FtbCultivateCourseChapterService ftbCultivateCourseChapterService;

    /**
     * 查询所有课程信息
     *
     * @return {@link ActionResult}
     */
    @GetMapping("/course/ai/query-list")
    public ActionResult<List<CultivateCourseAiVo>> list() {
        LambdaQueryWrapper<FtbCultivateCourse> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(FtbCultivateCourse::getId, FtbCultivateCourse::getName, FtbCultivateCourse::getHighLights)
                .eq(FtbCultivateCourse::getEnableMark, 0)
                .eq(FtbCultivateCourse::getIsGroundIng, 1)
                .orderByAsc(FtbCultivateCourse::getCreatorTime);

        List<FtbCultivateCourse> list = ftbCultivateCourseService.list(queryWrapper);
        if (CollUtil.isEmpty(list)) {
            return ActionResult.success("成功", Collections.emptyList());
        }


        // 查询所有章节并按课程 ID 分组
        Map<String, List<String>> courseChapterMap = buildCourseChapterMap();

        // 为缺少亮点的课程填充章节名称
        fillCourseHighLights(list, courseChapterMap);

        return ActionResult.success("成功", convertCultivateCourseAiVo(list));
    }

    /**
     * 构建课程与章节名称的映射关系
     *
     * @return 课程 ID 到章节名称列表的映射
     */
    private Map<String, List<String>> buildCourseChapterMap() {
        List<FtbCultivateCourseChapter> chapterList = queryAllCourseChapter();
        if (CollUtil.isEmpty(chapterList)) {
            return Collections.emptyMap();
        }

        return chapterList.stream()
                .collect(Collectors.groupingBy(
                        FtbCultivateCourseChapter::getCourseId,
                        Collectors.mapping(FtbCultivateCourseChapter::getName, Collectors.toList())
                ));
    }

    private void fillCourseHighLights(List<FtbCultivateCourse> courseList, Map<String, List<String>> courseChapterMap) {
        courseList.forEach(course -> {
            if (StringUtil.isEmpty(course.getHighLights())) {
                List<String> chapterNames = courseChapterMap.get(course.getId());
                if (CollUtil.isNotEmpty(chapterNames)) {
                    course.setHighLights(String.join(",", chapterNames));
                }
            }
        });
    }

    private List<FtbCultivateCourseChapter> queryAllCourseChapter() {
        LambdaQueryWrapper<FtbCultivateCourseChapter> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(FtbCultivateCourseChapter::getId, FtbCultivateCourseChapter::getName, FtbCultivateCourseChapter::getCourseId)
                .eq(FtbCultivateCourseChapter::getEnableMark, 0)
                .orderByAsc(FtbCultivateCourseChapter::getCreatorTime)
        ;
        List<FtbCultivateCourseChapter> list = ftbCultivateCourseChapterService.list(queryWrapper);
        if (CollUtil.isEmpty(list)) {
            return new ArrayList<>();
        }
        return list;
    }

    private List<CultivateCourseAiVo> convertCultivateCourseAiVo(List<FtbCultivateCourse> list) {
        return CollectionUtil.isEmpty(list) ? Collections.emptyList() : list.stream().map(item -> {
            CultivateCourseAiVo aiVo = new CultivateCourseAiVo();
            aiVo.setId(item.getId());
            aiVo.setName(item.getName());
            aiVo.setHighLights(item.getHighLights());
            return aiVo;
        }).collect(Collectors.toList());
    }


}
