package com.lesingle.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lesingle.edu.dto.response.ConflictCheckResult;
import com.lesingle.edu.entity.SchedulePlan;
import com.lesingle.edu.mapper.SchedulePlanMapper;
import com.lesingle.edu.service.ScheduleConflictService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 排课冲突检测服务实现
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ScheduleConflictServiceImpl implements ScheduleConflictService {

    private final SchedulePlanMapper schedulePlanMapper;

    @Override
    public ConflictCheckResult checkConflict(Long tenantId, Long classId, Long teacherId,
                                              LocalDate scheduledDate, String scheduledTime) {
        return checkConflict(tenantId, classId, teacherId, scheduledDate, scheduledTime, null);
    }

    @Override
    public ConflictCheckResult checkConflict(Long tenantId, Long classId, Long teacherId,
                                              LocalDate scheduledDate, String scheduledTime, Long excludeId) {
        log.debug("检测排课冲突: tenantId={}, classId={}, teacherId={}, date={}, time={}, excludeId={}",
                tenantId, classId, teacherId, scheduledDate, scheduledTime, excludeId);

        List<ConflictCheckResult.ConflictInfo> conflicts = new ArrayList<>();

        // 检查班级冲突
        LambdaQueryWrapper<SchedulePlan> classWrapper = new LambdaQueryWrapper<>();
        classWrapper.eq(SchedulePlan::getTenantId, tenantId)
                .eq(SchedulePlan::getClassId, classId)
                .eq(SchedulePlan::getScheduledDate, scheduledDate)
                .eq(SchedulePlan::getScheduledTime, scheduledTime)
                .ne(SchedulePlan::getStatus, "cancelled");

        if (excludeId != null) {
            classWrapper.ne(SchedulePlan::getId, excludeId);
        }

        List<SchedulePlan> classConflicts = schedulePlanMapper.selectList(classWrapper);
        for (SchedulePlan plan : classConflicts) {
            conflicts.add(ConflictCheckResult.ConflictInfo.builder()
                    .type("CLASS")
                    .message("班级在该时间段已有排课")
                    .conflictScheduleId(plan.getId())
                    .conflictTime(plan.getScheduledTime())
                    .build());
        }

        // 检查教师冲突
        LambdaQueryWrapper<SchedulePlan> teacherWrapper = new LambdaQueryWrapper<>();
        teacherWrapper.eq(SchedulePlan::getTenantId, tenantId)
                .eq(SchedulePlan::getTeacherId, teacherId)
                .eq(SchedulePlan::getScheduledDate, scheduledDate)
                .eq(SchedulePlan::getScheduledTime, scheduledTime)
                .ne(SchedulePlan::getStatus, "cancelled");

        if (excludeId != null) {
            teacherWrapper.ne(SchedulePlan::getId, excludeId);
        }

        List<SchedulePlan> teacherConflicts = schedulePlanMapper.selectList(teacherWrapper);
        for (SchedulePlan plan : teacherConflicts) {
            conflicts.add(ConflictCheckResult.ConflictInfo.builder()
                    .type("TEACHER")
                    .message("教师在该时间段已有排课")
                    .conflictScheduleId(plan.getId())
                    .conflictTime(plan.getScheduledTime())
                    .build());
        }

        if (conflicts.isEmpty()) {
            return ConflictCheckResult.noConflict();
        }

        return ConflictCheckResult.withConflicts(conflicts);
    }
}