package com.ruoyi.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.ruoyi.course.common.TeacherScheduleItem;
import com.ruoyi.course.common.TeacherTimetableResponse;
import com.ruoyi.course.domain.*;
import com.ruoyi.course.domain.Clazz;
import com.ruoyi.course.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class TeacherScheduleService {
    
    @Autowired
    private ScheduleItemMapper scheduleItemMapper;
    
    @Autowired
    private TeacherMapper teacherMapper;
    
    @Autowired
    private CourseMapper courseMapper;
    
    @Autowired
    private ClassroomMapper classroomMapper;
    
    @Autowired
    private ClazzMapper clazzMapper;
    
    @Autowired
    private TimeSlotMapper timeSlotMapper;
    
    public TeacherTimetableResponse getTeacherTimetable(Long teacherId, String semester) {
        TeacherTimetableResponse response = new TeacherTimetableResponse();
        
        // 1. 获取教师信息
        Teacher teacher = teacherMapper.selectById(teacherId);
        if (teacher == null) {
            throw new RuntimeException("教师不存在");
        }

        Teacher teacherInfo = new Teacher();
        teacherInfo.setId(teacher.getId());
        teacherInfo.setTeacherName(teacher.getTeacherName());
        teacherInfo.setTeacherNo(teacher.getTeacherNo());
        teacherInfo.setSubject(teacher.getSubject());
        response.setTeacher(teacher);
        response.setSemester(semester);
        
        // 2. 查询教师的排课项
        LambdaQueryWrapper<ScheduleItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ScheduleItem::getTeacherId, teacherId)
               .eq(ScheduleItem::getSemester, semester)
               .eq(ScheduleItem::getStatus, 1) // 已安排
               .orderByAsc(ScheduleItem::getDayOfWeek)
               .orderByAsc(ScheduleItem::getPeriod);
        
        List<ScheduleItem> scheduleItems = scheduleItemMapper.selectList(wrapper);
        response.setTotalHours(scheduleItems.size());
        
        // 3. 转换课表项
        List<TeacherScheduleItem> teacherScheduleItems = new ArrayList<>();
        for (ScheduleItem item : scheduleItems) {
            TeacherScheduleItem scheduleItem = convertToTeacherScheduleItem(item);
            teacherScheduleItems.add(scheduleItem);
        }
        response.setScheduleItems(teacherScheduleItems);
        
        // 4. 按天组织课表
        Map<String, List<TeacherScheduleItem>> dailySchedule = new HashMap<>();
        for (TeacherScheduleItem item : teacherScheduleItems) {
            String dayName = item.getDayName();
            dailySchedule.computeIfAbsent(dayName, k -> new ArrayList<>()).add(item);
        }
        response.setDailySchedule(dailySchedule);
        
        return response;
    }
    
    private TeacherScheduleItem convertToTeacherScheduleItem(ScheduleItem item) {
        TeacherScheduleItem teacherItem = new TeacherScheduleItem();
        
        // 基本时间信息
        teacherItem.setDayOfWeek(item.getDayOfWeek());
        teacherItem.setDayName(getDayName(item.getDayOfWeek()));
        teacherItem.setPeriod(item.getPeriod());
        
        // 获取时间槽信息
        TimeSlot timeSlot = timeSlotMapper.selectById(item.getTimeSlotId());
        if (timeSlot != null) {
            teacherItem.setStartTime(timeSlot.getStartTime().toString());
            teacherItem.setEndTime(timeSlot.getEndTime().toString());
        }
        
        // 课程信息
        Course course = courseMapper.selectById(item.getCourseId());
        if (course != null) {
            teacherItem.setCourseName(course.getCourseName());
            teacherItem.setCourseType(item.getCourseType());
        }
        
        // 教室信息
        Classroom classroom = classroomMapper.selectById(item.getClassroomId());
        if (classroom != null) {
            teacherItem.setClassroomName(classroom.getRoomName());
        }
        
        // 班级信息（如果是必修课）
        if (item.getClassId() != null) {
            Clazz clazz = clazzMapper.selectById(item.getClassId());
            if (clazz != null) {
                teacherItem.setClassName(clazz.getClassName());
            }
        } else {
            teacherItem.setClassName("走班选修");
        }
        
        teacherItem.setRemark(item.getRemark());
        
        return teacherItem;
    }
    
    private String getDayName(Integer dayOfWeek) {
        String[] days = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
        if (dayOfWeek >= 1 && dayOfWeek <= 7) {
            return days[dayOfWeek - 1];
        }
        return "未知";
    }
    

}