package cn.iocoder.byzs.module.aicourse.service.aiCourseConfig;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.byzs.framework.common.pojo.PageParam;
import cn.iocoder.byzs.framework.common.pojo.PageResult;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;
import cn.iocoder.byzs.module.aicourse.controller.admin.aiCourseConfig.vo.AiCourseConfigPageReqVO;
import cn.iocoder.byzs.module.aicourse.controller.admin.aiCourseConfig.vo.AiCourseConfigRespVO;
import cn.iocoder.byzs.module.aicourse.controller.admin.aiCourseConfig.vo.AiCourseConfigSaveReqVO;
import cn.iocoder.byzs.module.aicourse.dal.dataobject.aiCourseConfig.AiCourseConfigDO;
import cn.iocoder.byzs.module.aicourse.dal.mysql.aiCourseConfig.AiCourseConfigMapper;
import cn.iocoder.byzs.module.aicourse.service.aiCourseConfig.AiCourseConfigService;
import cn.iocoder.byzs.module.bjdx.controller.admin.coursequestion.vo.CourseQuestionRespVO;
import cn.iocoder.byzs.module.bjdx.service.coursequestion.CourseQuestionService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

import static cn.iocoder.byzs.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.byzs.module.aicourse.enums.ErrorCodeConstants.BLOCKLY_CONFIG_NOT_EXISTS;

/**
 * 课程配置 Service 实现类
 *
 * @author 博雅智算源码
 */
@Service
@Validated
public class AiCourseConfigServiceImpl implements AiCourseConfigService {

    @Resource
    private AiCourseConfigMapper aiCourseConfigMapper;
    @Resource
    private CourseQuestionService questionService;

    @Override
    public Long createAiCourseConfig(AiCourseConfigSaveReqVO createReqVO) {
        // 插入
        AiCourseConfigDO blocklyConfig = BeanUtils.toBean(createReqVO, AiCourseConfigDO.class);
        aiCourseConfigMapper.insert(blocklyConfig);
        // 返回
        return blocklyConfig.getId();
    }

    @Override
    public void updateAiCourseConfig(AiCourseConfigSaveReqVO updateReqVO) {
        // 校验存在
        validateAiCourseConfigExists(updateReqVO.getId());
        // 更新
        AiCourseConfigDO updateObj = BeanUtils.toBean(updateReqVO, AiCourseConfigDO.class);
        aiCourseConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteAiCourseConfig(Long id) {
        // 校验存在
        validateAiCourseConfigExists(id);
        // 删除
        aiCourseConfigMapper.deleteById(id);
    }

    @Override
        public void deleteAiCourseConfigListByIds(List<Long> ids) {
        // 校验存在
        validateAiCourseConfigExists(ids);
        // 删除
        aiCourseConfigMapper.deleteByIds(ids);
        }

    private void validateAiCourseConfigExists(List<Long> ids) {
        List<AiCourseConfigDO> list = aiCourseConfigMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BLOCKLY_CONFIG_NOT_EXISTS);
        }
    }

    private void validateAiCourseConfigExists(Long id) {
        if (aiCourseConfigMapper.selectById(id) == null) {
            throw exception(BLOCKLY_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public AiCourseConfigDO getAiCourseConfig(Long id) {
        return aiCourseConfigMapper.selectById(id);
    }

    @Override
    public List<AiCourseConfigRespVO> getAiCourseConfigQuestion(Long ccCourseId) {
        //课程配置列表
        AiCourseConfigPageReqVO blocklyConfigPageReqVO = new AiCourseConfigPageReqVO();
        blocklyConfigPageReqVO.setCcCourseId(Math.toIntExact(ccCourseId))
                .setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AiCourseConfigPageReqVO> blocklyConfigPageReqVOS = aiCourseConfigMapper.selectAiCourseConfigPage(blocklyConfigPageReqVO);

        //课程配置列表转VO
        List<AiCourseConfigRespVO> blocklyConfigList = blocklyConfigPageReqVOS.stream()
                .map(doItem -> BeanUtils.toBean(doItem, AiCourseConfigRespVO.class))
                .toList();
        if (blocklyConfigList.isEmpty()) {return blocklyConfigList;}

        //设置课程配置-试题列表(引用)
        Map<Long, CourseQuestionRespVO> questionMap = questionService.setCourseQuestionList(blocklyConfigList.stream().map(AiCourseConfigRespVO::getCcQuestId).collect(Collectors.toList()));
        blocklyConfigList.forEach(item -> item.setAiCourseQuestion(questionMap.get(item.getCcQuestId())));


        return blocklyConfigList;
    }

    @Override
    public List<AiCourseConfigRespVO> getAiCourseConfigQuestionByCourseIds(List<Long> ccCourseIds) {

        List<AiCourseConfigRespVO> blocklyConfigList = aiCourseConfigMapper.getAiCourseConfigQuestionByCourseIds(ccCourseIds);

        //设置课程配置-试题列表(引用)
        Map<Long, CourseQuestionRespVO> questionMap = questionService.setCourseQuestionList(blocklyConfigList.stream().map(AiCourseConfigRespVO::getCcQuestId).collect(Collectors.toList()));
        blocklyConfigList.forEach(item -> item.setAiCourseQuestion(questionMap.get(item.getCcQuestId())));

        return blocklyConfigList;
    }

    @Override
    public PageResult<AiCourseConfigPageReqVO> getAiCourseConfigPage(AiCourseConfigPageReqVO pageReqVO) {
        pageReqVO.setPageNo((pageReqVO.getPageNo() - 1) * pageReqVO.getPageSize());
        List<AiCourseConfigPageReqVO> blocklyConfigPageReqVOS = aiCourseConfigMapper.selectAiCourseConfigPage(pageReqVO);
        Integer count = aiCourseConfigMapper.selectAiCourseConfigCount(pageReqVO);

        // 调用服务层方法获取分页结果
        return new PageResult<>(
                blocklyConfigPageReqVOS.stream()
                       .map(doItem -> BeanUtils.toBean(doItem, AiCourseConfigPageReqVO.class))
                       .collect(Collectors.toList()),
                (long) count
        );
    }

}