package com.reading.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reading.platform.common.exception.BusinessException;
import com.reading.platform.entity.CoursePackageCourse;
import com.reading.platform.mapper.CoursePackageCourseMapper;
import com.reading.platform.service.CoursePackageCourseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 课程包课程关联服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CoursePackageCourseServiceImpl extends ServiceImpl<CoursePackageCourseMapper, CoursePackageCourse> implements CoursePackageCourseService {

    private final CoursePackageCourseMapper coursePackageCourseMapper;

    @Override
    public List<CoursePackageCourse> getPackageCourses(String coursePackageId) {
        log.debug("查询课程包包含的课程，课程包 ID: {}", coursePackageId);

        LambdaQueryWrapper<CoursePackageCourse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CoursePackageCourse::getCoursePackageId, coursePackageId);
        wrapper.orderByAsc(CoursePackageCourse::getSortOrder);

        List<CoursePackageCourse> result = coursePackageCourseMapper.selectList(wrapper);
        log.info("查询课程包包含的课程成功，课程包 ID: {}, 数量：{}", coursePackageId, result.size());
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CoursePackageCourse addPackageCourse(CoursePackageCourse relation) {
        log.info("开始添加课程包课程关联，课程包 ID: {}, 课程 ID: {}",
                relation.getCoursePackageId(), relation.getCourseId());

        relation.setCreatedAt(LocalDateTime.now());
        coursePackageCourseMapper.insert(relation);

        log.info("课程包课程关联添加成功，ID: {}", relation.getId());
        return relation;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removePackageCourse(String id) {
        log.info("开始移除课程包课程关联，ID: {}", id);

        CoursePackageCourse relation = getById(id);
        if (relation == null) {
            throw new BusinessException("课程包课程关联不存在");
        }

        coursePackageCourseMapper.deleteById(id);
        log.info("课程包课程关联移除成功，ID: {}", id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSortOrder(String id, Integer sortOrder) {
        log.info("开始更新课程包课程排序，ID: {}, 排序：{}", id, sortOrder);

        CoursePackageCourse relation = getById(id);
        if (relation == null) {
            throw new BusinessException("课程包课程关联不存在");
        }

        relation.setSortOrder(sortOrder);
        coursePackageCourseMapper.updateById(relation);

        log.info("课程包课程排序更新成功，ID: {}", id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchAddPackageCourses(String coursePackageId, List<String> courseIds) {
        log.info("开始批量添加课程包课程关联，课程包 ID: {}, 课程数量：{}", coursePackageId, courseIds.size());

        for (int i = 0; i < courseIds.size(); i++) {
            CoursePackageCourse relation = new CoursePackageCourse();
            relation.setCoursePackageId(coursePackageId);
            relation.setCourseId(courseIds.get(i));
            relation.setSortOrder(i);
            relation.setCreatedAt(LocalDateTime.now());
            coursePackageCourseMapper.insert(relation);
        }

        log.info("批量添加课程包课程关联成功，课程包 ID: {}, 数量：{}", coursePackageId, courseIds.size());
    }
}
