package com.lesingle.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lesingle.edu.common.response.PageResult;
import com.lesingle.edu.dto.response.LessonRecordResponse;
import com.lesingle.edu.entity.CoursePackage;
import com.lesingle.edu.entity.Lesson;
import com.lesingle.edu.entity.StudentRecord;
import com.lesingle.edu.mapper.CoursePackageMapper;
import com.lesingle.edu.mapper.LessonMapper;
import com.lesingle.edu.mapper.StudentRecordMapper;
import com.lesingle.edu.service.ParentAuthService;
import com.lesingle.edu.service.ParentLessonService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 家长端阅读记录服务实现
 */
@Service
@RequiredArgsConstructor
public class ParentLessonServiceImpl implements ParentLessonService {

    private final StudentRecordMapper studentRecordMapper;
    private final LessonMapper lessonMapper;
    private final CoursePackageMapper coursePackageMapper;
    private final ParentAuthService parentAuthService;

    @Override
    public PageResult<LessonRecordResponse> getLessonsByStudentId(Long studentId, Long parentId, Integer pageNum, Integer pageSize) {
        parentAuthService.validateParentStudentRelation(parentId, studentId);

        Page<StudentRecord> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<StudentRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentRecord::getStudentId, studentId)
                .orderByDesc(StudentRecord::getCreatedAt);
        Page<StudentRecord> recordPage = studentRecordMapper.selectPage(page, wrapper);

        List<LessonRecordResponse> responses = new ArrayList<>();
        for (StudentRecord record : recordPage.getRecords()) {
            Lesson lesson = lessonMapper.selectById(record.getLessonId());
            if (lesson == null) {
                continue;
            }

            LessonRecordResponse.LessonInfo.LessonInfoBuilder lessonInfoBuilder = LessonRecordResponse.LessonInfo.builder()
                    .id(lesson.getId())
                    .startDatetime(lesson.getStartDatetime())
                    .endDatetime(lesson.getEndDatetime())
                    .actualDuration(lesson.getActualDuration());

            if (lesson.getCourseId() != null) {
                CoursePackage course = coursePackageMapper.selectById(lesson.getCourseId());
                if (course != null) {
                    lessonInfoBuilder.course(LessonRecordResponse.CourseInfo.builder()
                            .id(course.getId())
                            .name(course.getName())
                            .pictureBookName(course.getPictureBookName())
                            .build());
                }
            }

            responses.add(LessonRecordResponse.builder()
                    .id(record.getId())
                    .lesson(lessonInfoBuilder.build())
                    .focus(record.getFocus())
                    .participation(record.getParticipation())
                    .interest(record.getInterest())
                    .understanding(record.getUnderstanding())
                    .notes(record.getNotes())
                    .createdAt(record.getCreatedAt())
                    .build());
        }

        return PageResult.of(responses, recordPage.getTotal(), recordPage.getCurrent(), recordPage.getSize());
    }
}
