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.entity.Lesson;
import com.lesingle.edu.entity.LessonFeedback;
import com.lesingle.edu.mapper.LessonFeedbackMapper;
import com.lesingle.edu.mapper.LessonMapper;
import com.lesingle.edu.service.TeacherFeedbackService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * 教师端反馈服务实现
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TeacherFeedbackServiceImpl implements TeacherFeedbackService {

    private final LessonFeedbackMapper lessonFeedbackMapper;
    private final LessonMapper lessonMapper;

    @Override
    public Page<LessonFeedback> getTeacherFeedbacks(Long teacherId, Integer pageNum, Integer pageSize) {
        log.debug("获取教师反馈列表，教师 ID: {}, 页码：{}", teacherId, pageNum);

        Page<LessonFeedback> page = new Page<>(pageNum, pageSize);

        // 先获取教师的所有课程
        List<Lesson> lessons = lessonMapper.selectList(
                new LambdaQueryWrapper<Lesson>()
                        .eq(Lesson::getTeacherId, teacherId)
        );

        if (lessons.isEmpty()) {
            return page;
        }

        List<Long> lessonIds = lessons.stream().map(Lesson::getId).collect(Collectors.toList());

        LambdaQueryWrapper<LessonFeedback> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(LessonFeedback::getLessonId, lessonIds)
                .orderByDesc(LessonFeedback::getCreatedAt);

        return lessonFeedbackMapper.selectPage(page, wrapper);
    }

    @Override
    public Map<String, Object> getFeedbackStats(Long teacherId) {
        log.debug("获取教师反馈统计，教师 ID: {}", teacherId);

        Map<String, Object> stats = new HashMap<>();

        // 获取教师的所有课程
        List<Lesson> lessons = lessonMapper.selectList(
                new LambdaQueryWrapper<Lesson>()
                        .eq(Lesson::getTeacherId, teacherId)
        );

        if (lessons.isEmpty()) {
            stats.put("totalFeedbacks", 0);
            stats.put("avgRating", 0.0);
            stats.put("byRating", new HashMap<>());
            return stats;
        }

        List<Long> lessonIds = lessons.stream().map(Lesson::getId).collect(Collectors.toList());

        // 获取反馈列表
        List<LessonFeedback> feedbacks = lessonFeedbackMapper.selectList(
                new LambdaQueryWrapper<LessonFeedback>()
                        .in(LessonFeedback::getLessonId, lessonIds)
        );

        stats.put("totalFeedbacks", feedbacks.size());

        // 计算平均评分
        double avgRating = feedbacks.stream()
                .filter(f -> f.getRating() != null)
                .mapToInt(LessonFeedback::getRating)
                .average()
                .orElse(0.0);
        stats.put("avgRating", avgRating);

        // 按评分分组统计
        Map<Integer, Long> byRating = feedbacks.stream()
                .filter(f -> f.getRating() != null)
                .collect(Collectors.groupingBy(LessonFeedback::getRating, Collectors.counting()));
        stats.put("byRating", byRating);

        return stats;
    }

}
