package com.yinlihupo.enlish.service.controller;

import com.yinlihupo.enlish.service.domain.dataobject.LessonPlansDO;
import com.yinlihupo.enlish.service.domain.dataobject.StudentLessonPlansDO;
import com.yinlihupo.enlish.service.model.bo.StudentDetail;
import com.yinlihupo.enlish.service.model.vo.plan.*;
import com.yinlihupo.enlish.service.service.LessonPlansService;
import com.yinlihupo.enlish.service.service.StudentLessonPlansService;
import com.yinlihupo.enlish.service.service.StudentService;
import com.yinlihupo.framework.biz.operationlog.aspect.ApiOperationLog;
import com.yinlihupo.framework.common.response.PageResponse;
import com.yinlihupo.framework.common.response.Response;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RequestMapping("/studentLessonPlans/")
@RestController
public class StudentLessonPlansController {

    @Resource
    private StudentLessonPlansService studentLessonPlansService;
    @Resource
    private StudentService studentService;
    @Resource
    private LessonPlansService lessonPlansService;

    @PostMapping("/list")
    @ApiOperationLog(description = "查询学生学案")
    public PageResponse<FindStudentPlansRspVO> findStudentPlans(@RequestBody FindStudentPlansReqVO findStudentPlansReqVO) {

        Integer studentLessonPlanTotal = studentLessonPlansService.findStudentLessonPlanTotal();

        String name = findStudentPlansReqVO.getName();
        Integer page = findStudentPlansReqVO.getPage();
        Integer size = findStudentPlansReqVO.getSize();

        List<StudentLessonPlansDO> studentLessonPlansDOListPageSize = studentLessonPlansService.findStudentLessonPlansDOList(page, size, name);
        Map<Integer, List<StudentLessonPlansDO>> studentId2StudentLessonPlansDOListMap = studentLessonPlansDOListPageSize.stream().collect(
                Collectors.groupingBy(StudentLessonPlansDO::getStudentId)
        );

        List<Integer> planIds = studentLessonPlansDOListPageSize.stream().map(StudentLessonPlansDO::getPlanId).toList();
        List<LessonPlansDO> lessonPlans = lessonPlansService.findLessonPlans(planIds);
        Map<Integer, LessonPlansDO> id2LessonPlansDO = lessonPlans.stream().collect(Collectors.toMap(
                LessonPlansDO::getId,
                lessonPlansDO -> lessonPlansDO
        ));

        List<StudentDetail> studentDetailList = studentService.getStudentDetailList(new ArrayList<>(studentId2StudentLessonPlansDOListMap.keySet()));

        List<FindStudentPlansRspVO> findStudentPlansRspVOList = studentDetailList.stream().map(studentDetail -> {
            List<StudentLessonPlansDO> studentLessonPlansDOList = studentId2StudentLessonPlansDOListMap.get(studentDetail.getId());
            return FindStudentPlansRspVO.builder()
                    .name(studentDetail.getName())
                    .id(studentDetail.getId())
                    .classId(studentDetail.getClassId())
                    .gradeId(studentDetail.getGradeId())
                    .gradeName(studentDetail.getGradeName())
                    .className(studentDetail.getClassName())
                    .plans(studentLessonPlansDOList.stream().map(studentLessonPlansDO ->
                        LessonPlanItem.builder().title(id2LessonPlansDO.get(studentLessonPlansDO.getPlanId()).getTitle()).id(studentLessonPlansDO.getPlanId()).isFinished(studentLessonPlansDO.getIsFinished()).build()
                    ).toList())
                    .build();
        }).toList();

        return PageResponse.success(findStudentPlansRspVOList, page, studentLessonPlanTotal, size);
    }

    @PostMapping("finish")
    @ApiOperationLog(description = "完成学案")
    public Response<String> finishStudentPlan(@RequestBody FinishStudentPlanReqVO finishStudentPlanReqVO) {
        Integer studentId = finishStudentPlanReqVO.getStudentId();
        Integer planId = finishStudentPlanReqVO.getPlanId();
        int lessonPlan = studentLessonPlansService.finishStudentLessonPlan(studentId, planId);

        if (lessonPlan > 0) {
            return Response.success("完成学案成功");
        }
        return Response.fail("完成学案失败");
    }

    @PostMapping("history")
    @ApiOperationLog(description = "查询学案历史")
    public Response<List<FindStudentPlanHistoryListRspVO>> findStudentPlanHistory(@RequestBody FindStudentPlanHistoryReqVO findStudentPlanHistoryReqVO) {
        Integer studentId = findStudentPlanHistoryReqVO.getStudentId();
        List<FindStudentPlanHistoryListRspVO> list = studentLessonPlansService.findStudentLessonPlansByStudentId(studentId)
                .stream().map(studentLessonPlansDO -> FindStudentPlanHistoryListRspVO.builder()
                .id(studentLessonPlansDO.getId())
                .studentId(studentLessonPlansDO.getStudentId())
                .planId(studentLessonPlansDO.getPlanId())
                .startTime(studentLessonPlansDO.getStartTime())
                .masteryRat(studentLessonPlansDO.getMasteryRat())
                .totalCount(studentLessonPlansDO.getTotalCount())
                .build()
        ).toList();

        return Response.success(list);
    }
}
