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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seer.teach.common.enums.MakeQuestionTypeEnums;
import com.seer.teach.teacher.common.req.WrongQuestionDetailReq;
import com.seer.teach.teacher.module.entity.BankQuestionsEntity;
import com.seer.teach.teacher.module.entity.EvaluationQuestionsEntity;
import com.seer.teach.teacher.service.BankQuestionsService;
import com.seer.teach.teacher.service.EvaluationQuestionsService;
import com.seer.teach.teacher.service.registry.wrongQuestionDetail.WrongQuestionDetailHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 错题详情处理器实现类，用于处理测评类型的错题详情
 * </p>
 *
 * @author Captain
 * @since 2025-08-12 21:19
 */
@Service
@RequiredArgsConstructor
public class EvaluationWrongQuestionDetailHandlerImpl implements WrongQuestionDetailHandler {

    private final EvaluationQuestionsService evaluationQuestionsService;

    private final BankQuestionsService bankQuestionsService;


    /**
     * 处理错题详情，根据任务ID查询相关题目信息，并生成包含题目、选项、解题过程和答案的HTML内容列表。
     *
     * @param userId 用户ID（当前未使用）
     * @param params 请求参数对象，包含任务ID等信息
     * @return 包含每道题目的完整HTML内容的字符串列表
     */
    @Override
    public List<String> handler(Integer userId, WrongQuestionDetailReq params) {
        // 查询该任务下的所有题目
        List<Integer> questionIds = evaluationQuestionsService.list(new LambdaQueryWrapper<EvaluationQuestionsEntity>()
                .eq(EvaluationQuestionsEntity::getTaskId, params.getId())).stream()
                .map(EvaluationQuestionsEntity::getQuestionId).collect(Collectors.toList());
        List<BankQuestionsEntity> questions = bankQuestionsService.listByIds(questionIds);
        return questions.stream().map(BankQuestionsEntity::getHtml).collect(Collectors.toList());
    }

    /**
     * 获取处理类型标识，用于区分不同的错题处理实现类。
     *
     * @return 错题处理类型的编码值
     */
    @Override
    public Integer getType() {
        return MakeQuestionTypeEnums.EVALUATION.getCode();
    }
}
