package com.lesingle.creation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lesingle.creation.dto.presetcomment.CreatePresetCommentDTO;
import com.lesingle.creation.entity.ContestPresetComment;
import com.lesingle.creation.common.exception.BusinessException;
import com.lesingle.creation.mapper.ContestPresetCommentMapper;
import com.lesingle.creation.service.ContestPresetCommentService;
import com.lesingle.creation.vo.presetcomment.PresetCommentVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 竞赛预设评语服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ContestPresetCommentServiceImpl implements ContestPresetCommentService {

    private final ContestPresetCommentMapper presetCommentMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PresetCommentVO create(CreatePresetCommentDTO dto, Long tenantId) {
        ContestPresetComment comment = new ContestPresetComment();
        comment.setTenantId(tenantId);
        comment.setContestId(dto.getContestId());
        comment.setContent(dto.getContent());
        comment.setType(dto.getType());
        comment.setScoreRangeMin(dto.getScoreRangeMin());
        comment.setScoreRangeMax(dto.getScoreRangeMax());
        comment.setSort(dto.getSort());
        comment.setUsageCount(0);

        presetCommentMapper.insert(comment);
        log.info("创建预设评语成功，ID={}", comment.getId());

        return convertToVO(comment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        ContestPresetComment comment = presetCommentMapper.selectById(id);
        if (comment == null) {
            throw new BusinessException("预设评语不存在");
        }
        presetCommentMapper.deleteById(id);
        log.info("删除预设评语成功，ID={}", id);
    }

    @Override
    public PresetCommentVO getDetail(Long id) {
        ContestPresetComment comment = presetCommentMapper.selectById(id);
        if (comment == null) {
            throw new BusinessException("预设评语不存在");
        }
        return convertToVO(comment);
    }

    @Override
    public List<PresetCommentVO> listByContest(Long contestId, Long tenantId) {
        List<ContestPresetComment> comments = presetCommentMapper.selectList(new LambdaQueryWrapper<ContestPresetComment>()
                .eq(ContestPresetComment::getTenantId, tenantId)
                .eq(ContestPresetComment::getContestId, contestId)
                .eq(ContestPresetComment::getDeleted, 0)
                .orderByAsc(ContestPresetComment::getSort));

        return comments.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    public List<PresetCommentVO> listCommon(Long tenantId) {
        List<ContestPresetComment> comments = presetCommentMapper.selectList(new LambdaQueryWrapper<ContestPresetComment>()
                .eq(ContestPresetComment::getTenantId, tenantId)
                .isNull(ContestPresetComment::getContestId)
                .eq(ContestPresetComment::getDeleted, 0)
                .orderByAsc(ContestPresetComment::getSort));

        return comments.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    // ========== 转换方法 ==========

    private PresetCommentVO convertToVO(ContestPresetComment comment) {
        PresetCommentVO vo = new PresetCommentVO();
        vo.setId(comment.getId());
        vo.setTenantId(comment.getTenantId());
        vo.setContestId(comment.getContestId());
        vo.setContent(comment.getContent());
        vo.setType(comment.getType());
        vo.setScoreRangeMin(comment.getScoreRangeMin());
        vo.setScoreRangeMax(comment.getScoreRangeMax());
        vo.setSort(comment.getSort());
        vo.setUsageCount(comment.getUsageCount());
        return vo;
    }
}
