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.ProductBundleCoursePackage;
import com.reading.platform.mapper.ProductBundleCoursePackageMapper;
import com.reading.platform.service.ProductBundleCoursePackageService;
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 ProductBundleCoursePackageServiceImpl extends ServiceImpl<ProductBundleCoursePackageMapper, ProductBundleCoursePackage> implements ProductBundleCoursePackageService {

    private final ProductBundleCoursePackageMapper productBundleCoursePackageMapper;

    @Override
    public List<ProductBundleCoursePackage> getBundleCoursePackages(String productBundleId) {
        log.debug("查询套餐包含的课程包，套餐 ID: {}", productBundleId);

        LambdaQueryWrapper<ProductBundleCoursePackage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductBundleCoursePackage::getProductBundleId, productBundleId);
        wrapper.orderByAsc(ProductBundleCoursePackage::getSortOrder);

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

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

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

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

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

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

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

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

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

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

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

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

        for (int i = 0; i < coursePackageIds.size(); i++) {
            ProductBundleCoursePackage relation = new ProductBundleCoursePackage();
            relation.setProductBundleId(productBundleId);
            relation.setCoursePackageId(coursePackageIds.get(i));
            relation.setSortOrder(i);
            relation.setCreatedAt(LocalDateTime.now());
            productBundleCoursePackageMapper.insert(relation);
        }

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