package com.seer.teach.teacher.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.teacher.api.dto.CurrentAndFutureSubjectsDTO;
import com.seer.teach.teacher.api.dto.SubjectDTO;
import com.seer.teach.teacher.convert.SubjectConvert;
import com.seer.teach.teacher.module.entity.GradeSubjectEntity;
import com.seer.teach.teacher.module.entity.SubjectsEntity;
import com.seer.teach.teacher.service.GradeSubjectService;
import com.seer.teach.teacher.service.SubjectsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p >
 *
 * @author Captain
 * @since 2025-08-04 19:04
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class SubjectServiceApiImpl implements SubjectServiceApi {

    private final SubjectsService subjectsService;

    private final GradeSubjectService gradeSubjectService;

    /**
     * 获取指定年级的当前和以后年级科目信息
     *
     * @param gradeIds 年级ID列表
     * @return 按年级分组的当前和以后科目信息列表
     */
    @Override
    public List<CurrentAndFutureSubjectsDTO> getCurrentAndFutureSubjects(List<Integer> gradeIds) {

        // 1. 查询年级与科目的关联数据，按 gradeId 分组
        Map<Integer, List<GradeSubjectEntity>> gradeSubjectsMap = gradeSubjectService.list(new LambdaQueryWrapper<GradeSubjectEntity>()
                        .in(GradeSubjectEntity::getGradeId, gradeIds)
                        .orderByAsc(GradeSubjectEntity::getSort)).stream()
                .collect(Collectors.groupingBy(GradeSubjectEntity::getGradeId, Collectors.toList()));

        // 2. 提取所有的科目ID并去重
        Set<Integer> allSubjectIds = gradeSubjectsMap.values().stream()
                .flatMap(List::stream)
                .map(GradeSubjectEntity::getSubjectId)
                .collect(Collectors.toSet());

        // 3. 批量查询所有科目详情并构建 subjectId -> SubjectsEntity 的映射
        Map<Integer, SubjectsEntity> subjectEntityMap = subjectsService.list(new LambdaQueryWrapper<SubjectsEntity>()
                        .in(SubjectsEntity::getId, allSubjectIds)).stream()
                .collect(Collectors.toMap(SubjectsEntity::getId, Function.identity(), (a, b) -> a
        ));

        // 4. 构建结果 DTO 列表
        return gradeIds.stream().map(gradeId -> {
            List<GradeSubjectEntity> subjectEntitiesForGrade = gradeSubjectsMap.getOrDefault(gradeId, Collections.emptyList());

            List<SubjectDTO> subjectDTOList = subjectEntitiesForGrade.stream()
                    .map(GradeSubjectEntity::getSubjectId)
                    .map(subjectEntityMap::get)
                    .filter(Objects::nonNull)
                    .map(SubjectConvert.INSTANCE::toSubjectDTO)
                    .collect(Collectors.toList());

            return SubjectConvert.INSTANCE.toCurrentAndFutureSubjectsDTO(gradeId, subjectDTOList);
        }).collect(Collectors.toList());
    }

    /**
     * 获取指定科目ID的科目信息
     * @param subjectId
     * @return
     */
    @Override
    public String getBySubjectId(Integer subjectId) {
        SubjectsEntity subjectsEntity = subjectsService.getById(subjectId);
        AssertUtils.notNull(subjectsEntity, ResultCodeEnum.SUBJECT_IS_NOT_EXIST);
        return subjectsEntity.getSubject();
    }

    @Override
    public List<SubjectDTO> getSubjectlist() {
        List<SubjectsEntity> list = subjectsService.list();
        return SubjectConvert.INSTANCE.toSubjectDTOList(list);
    }

}
