package com.seer.teach.teacher.service.impl;

import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.teacher.common.dto.TeachAudios;
import com.seer.teach.teacher.common.dto.TeachDTO;
import com.seer.teach.teacher.convert.TeachConvert;
import com.seer.teach.teacher.module.entity.*;
import com.seer.teach.teacher.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * AI场景定义表 服务实现类
 * </p>
 *
 * @author System
 * @since 2025-10-27
 */
@Service
@RequiredArgsConstructor
public class TeachServiceImpl implements TeachService {

    private final KnowledgePointService knowledgePointService;

    private final KnowSummaryService knowSummaryService;

    private final TeachGoalsService teachGoalsService;

    private final TeachTextsService teachTextsService;

    private final TeachTextAudiosService teachTextAudiosService;

    @Override
    public TeachDTO firstLesson(Integer knowId) {
        KnowledgePointEntity knowledgePoint = knowledgePointService.getById(knowId);
        KnowSummaryEntity summary = knowSummaryService.getOneByKnowId(knowledgePoint.getId());
        TeachGoalsEntity goal = teachGoalsService.getFirstGoalByKnowId(knowledgePoint.getId());
        // TODO 有了用户画像可以根据用户画像喜欢的教学方式进行查询
        TeachTextsEntity text = teachTextsService.getDefaultTextByGoalId(goal.getId());
        List<TeachTextAudiosEntity> audios = teachTextAudiosService.getTextAudiosByTextId(text.getId());
        TeachDTO result = TeachConvert.INSTANCE.toTeachDTO(knowledgePoint, summary, goal, text);
        List<TeachAudios> teachAudios = audios.stream()
                .map(audio -> {
                    TeachAudios audioResult = TeachConvert.INSTANCE.toTeachAudios(audio);
                    audioResult.setIsFinal(audio.getIsFinal().equals(1));
                    return audioResult;
                })
                .collect(Collectors.toList());
        result.setAudios(teachAudios);
        return result;
    }

    @Override
    public List<TeachAudios> getAudiosByTextIdAndSort(Integer textId, Integer sort) {
        List<TeachTextAudiosEntity> audios = teachTextAudiosService.lambdaQuery()
                .eq(TeachTextAudiosEntity::getTextId, textId)
                .ge(TeachTextAudiosEntity::getSort, sort)
                .list();
        return audios.stream()
                .map(audio -> {
                    TeachAudios audioResult = TeachConvert.INSTANCE.toTeachAudios(audio);
                    audioResult.setIsFinal(audio.getIsFinal().equals(1));
                    return audioResult;
                })
                .collect(Collectors.toList());
    }

    @Override
    public void nextLesson(TeachDTO teachDTO) {
        TeachGoalsEntity goal = teachGoalsService.lambdaQuery()
                .eq(TeachGoalsEntity::getKnowId, teachDTO.getId())
                .eq(TeachGoalsEntity::getSort, teachDTO.getSort() + 1)
                .one();
        TeachTextsEntity text = teachTextsService.getDefaultTextByGoalId(goal.getId());
        List<TeachTextAudiosEntity> audios = teachTextAudiosService.getTextAudiosByTextId(text.getId());
        List<TeachAudios> teachAudios = audios.stream()
                .map(audio -> {
                    TeachAudios audioResult = TeachConvert.INSTANCE.toTeachAudios(audio);
                    audioResult.setIsFinal(audio.getIsFinal().equals(1));
                    return audioResult;
                })
                .collect(Collectors.toList());
        teachDTO.setAudios(teachAudios);
        teachDTO.setGoal(goal.getGoal());
        teachDTO.setGoalId(goal.getId());
        teachDTO.setPoint(goal.getPoint());
        teachDTO.setText(text.getText());
        teachDTO.setTextId(text.getId());
        teachDTO.setSort(goal.getSort());
        teachDTO.setVideoUrl(text.getVideoUrl());
    }

    /**
     * 检查指定知识点和排序位置是否为最后一个教学目标
     *
     * @param knowId 知识点ID
     * @param sort   当前排序位置
     * @return 如果是最后一个教学目标返回true，否则返回false
     */
    @Override
    public Boolean checkIsLastOneGoal(Integer knowId, Integer sort) {
        // 查询下一个排序位置的教学目标是否存在
        TeachGoalsEntity one = teachGoalsService.lambdaQuery()
                .eq(TeachGoalsEntity::getKnowId, knowId)
                .eq(TeachGoalsEntity::getSort, sort + 1)
                .one();
        // 如果不存在下一个排序位置的教学目标，则当前为最后一个
        return one == null;
    }


}