package com.yinlihupo.enlish.service.controller;


import com.yinlihupo.enlish.service.domain.dataobject.ClassDO;
import com.yinlihupo.enlish.service.domain.dataobject.GradeDO;
import com.yinlihupo.enlish.service.domain.dataobject.StudentDO;
import com.yinlihupo.enlish.service.model.bo.StudentDetail;
import com.yinlihupo.enlish.service.model.bo.exam.WordMasteryDetail;
import com.yinlihupo.enlish.service.model.vo.student.*;
import com.yinlihupo.enlish.service.service.ClassService;
import com.yinlihupo.enlish.service.service.GradeService;
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.List;

@RequestMapping("/student/")
@RestController
public class StudentController {

    @Resource
    private StudentService studentService;
    @Resource
    private ClassService classService;
    @Resource
    private GradeService gradeService;

    @PostMapping("/list")
    @ApiOperationLog(description = "查询学生列表")
    public PageResponse<StudentItemRspVO> findStudents(@RequestBody FindStudentsReqVO findStudentsReqVO) {

        Integer classId = findStudentsReqVO.getClassId();
        Integer gradeId = findStudentsReqVO.getGradeId();
        Integer pageNo = findStudentsReqVO.getPageNo();
        Integer pageSize = findStudentsReqVO.getPageSize();
        String name = findStudentsReqVO.getName();
        List<StudentDO> students = studentService.getStudentsByClassIdAndGradeId(classId, gradeId, name, pageNo, pageSize);
        int allStudents = studentService.getAllStudents();

        List<StudentItemRspVO> studentItemRspVOS = students.stream().map(studentDO -> StudentItemRspVO.builder()
                .id(studentDO.getId())
                .name(studentDO.getName())
                .classId(studentDO.getClassId())
                .className(classService.findClassById(studentDO.getClassId()).getTitle())
                .gradeName(gradeService.findByClassId(studentDO.getGradeId()).getTitle())
                .gradeId(studentDO.getGradeId())
                .build()).toList();

        return PageResponse.success(studentItemRspVOS, pageNo, allStudents, pageSize);
    }

    @PostMapping("/detail")
    @ApiOperationLog(description = "查询学生详情")
    public Response<FindStudentDetailRspVO> findStudentDetail(@RequestBody FindStudentDetailReqVO findStudentDetailReqVO) {
        Integer studentId = findStudentDetailReqVO.getStudentId();

        StudentDO studentById = studentService.getStudentById(studentId);
        ClassDO classById = classService.findClassById(studentById.getClassId());
        GradeDO byClassId = gradeService.findByClassId(studentById.getGradeId());
        GradeDO actualGradeById = gradeService.findByClassId(studentById.getActualGradeId());
        FindStudentDetailRspVO findStudentDetailRspVO = FindStudentDetailRspVO.builder()
                .id(studentById.getId())
                .name(studentById.getName())
                .className(classById.getTitle())
                .gradeName(byClassId.getTitle())
                .actualGrade(actualGradeById != null ? actualGradeById.getTitle() : "")
                .build();

        return Response.success(findStudentDetailRspVO);
    }

    @PostMapping("add")
    @ApiOperationLog(description = "添加学生")
    public Response<Void> addStudent(@RequestBody AddStudentReqVO addStudentReqVO) {
        studentService.addStudent(addStudentReqVO);
        return Response.success();
    }

    @PostMapping("delete")
    @ApiOperationLog(description = "删除学生")
    public Response<Void> deleteStudent(@RequestBody DeleteStudentReqVO deleteStudentReqVO) {
        studentService.deleteStudent(deleteStudentReqVO.getStudentId());
        return Response.success();
    }

    @PostMapping("analyze")
    @ApiOperationLog(description = "学生学习分析")
    public Response<String> analyzeStudentStudy(@RequestBody AnalyzeStudentStudyReqVO analyzeStudentStudyReqVO) {
        String analyzeStudentStudy = studentService.analyzeStudentStudy(analyzeStudentStudyReqVO.getStudentId());
        return Response.success(analyzeStudentStudy);
    }

    @PostMapping("mastery/detail")
    @ApiOperationLog(description = "查询学生单词掌握详情")
    public Response<List<FindStudentMasteryDetailRspVO>> findStudentMasteryDetail(@RequestBody FindStudentMasteryDetailReqVO findStudentMasteryDetailReqVO) {
        Integer studentId = findStudentMasteryDetailReqVO.getStudentId();
        List<WordMasteryDetail> studentWordMasteryDetail = studentService.findStudentWordMasteryDetail(studentId);

        List<FindStudentMasteryDetailRspVO> list = studentWordMasteryDetail.stream().map(wordMasteryDetail -> FindStudentMasteryDetailRspVO.builder()
                .word(wordMasteryDetail.getWord())
                .reviewCount(wordMasteryDetail.getReviewCount())
                .memoryStrength(wordMasteryDetail.getMemoryStrength())
                .updateTime(wordMasteryDetail.getUpdate_time())
                .build()
        ).toList();

        return Response.success(list);
    }
}
