package com.snack.server.module.chat.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.snack.server.common.ResultCode;
import com.snack.server.exception.BusinessException;
import com.snack.server.module.chat.dto.req.QuickReplySaveReq;
import com.snack.server.module.chat.entity.QuickReply;
import com.snack.server.module.chat.mapper.QuickReplyMapper;
import com.snack.server.module.chat.service.QuickReplyService;
import com.snack.server.module.chat.vo.QuickReplyVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class QuickReplyServiceImpl implements QuickReplyService {

    private final QuickReplyMapper quickReplyMapper;

    @Override
    public List<QuickReplyVO> listActiveReplies(String category) {
        List<QuickReply> list = quickReplyMapper.selectList(
                new LambdaQueryWrapper<QuickReply>()
                        .eq(QuickReply::getStatus, 1)
                        .eq(StrUtil.isNotBlank(category), QuickReply::getCategory, category)
                        .orderByAsc(QuickReply::getSort)
        );
        return list.stream().map(this::toVO).toList();
    }

    @Override
    public QuickReplyVO getById(Long id) {
        QuickReply q = quickReplyMapper.selectById(id);
        if (q == null) throw new BusinessException(ResultCode.PARAM_ERROR.getCode(), "模板不存在");
        return toVO(q);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(QuickReplySaveReq req, Long createBy) {
        QuickReply q = new QuickReply();
        BeanUtil.copyProperties(req, q);
        q.setUseCount(0);
        q.setCreateBy(createBy);
        quickReplyMapper.insert(q);
        return q.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(QuickReplySaveReq req) {
        if (req.getId() == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR.getCode(), "ID 不能为空");
        }
        QuickReply q = quickReplyMapper.selectById(req.getId());
        if (q == null) throw new BusinessException(ResultCode.PARAM_ERROR.getCode(), "模板不存在");
        BeanUtil.copyProperties(req, q);
        quickReplyMapper.updateById(q);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        QuickReply q = quickReplyMapper.selectById(id);
        if (q == null) return;
        quickReplyMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrUseCount(Long id) {
        QuickReply q = quickReplyMapper.selectById(id);
        if (q == null) return;
        q.setUseCount(q.getUseCount() == null ? 1 : q.getUseCount() + 1);
        quickReplyMapper.updateById(q);
    }

    private QuickReplyVO toVO(QuickReply q) {
        QuickReplyVO vo = new QuickReplyVO();
        BeanUtil.copyProperties(q, vo);
        return vo;
    }
}
