package cn.meowrain.aioj.backend.blogservice.service.impl;

import cn.meowrain.aioj.backend.blogservice.dao.entity.Like;
import cn.meowrain.aioj.backend.blogservice.dao.mapper.LikeMapper;
import cn.meowrain.aioj.backend.blogservice.dto.req.LikeRequestDTO;
import cn.meowrain.aioj.backend.blogservice.service.LikeService;
import cn.meowrain.aioj.backend.framework.core.exception.ClientException;
import cn.meowrain.aioj.backend.framework.core.utils.ContextHolderUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 点赞服务实现类
 *
 * @author AIOJ
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Like> implements LikeService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean like(LikeRequestDTO request) {
        Long currentUserId = ContextHolderUtils.getCurrentUserId();

        // 检查是否已点赞
        Like existingLike = this.lambdaQuery()
                .eq(Like::getUserId, currentUserId)
                .eq(Like::getTargetId, request.getTargetId())
                .eq(Like::getTargetType, request.getTargetType())
                .eq(Like::getIsCancelled, 0)
                .one();

        if (existingLike != null) {
            throw new ClientException("已经点赞过了");
        }

        // 创建点赞记录
        Like like = new Like()
                .setUserId(currentUserId)
                .setTargetId(request.getTargetId())
                .setTargetType(request.getTargetType())
                .setIsCancelled(0);
        this.save(like);

        // TODO: 更新文章或评论的点赞数

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean unlike(LikeRequestDTO request) {
        Long currentUserId = ContextHolderUtils.getCurrentUserId();

        Like like = this.lambdaQuery()
                .eq(Like::getUserId, currentUserId)
                .eq(Like::getTargetId, request.getTargetId())
                .eq(Like::getTargetType, request.getTargetType())
                .eq(Like::getIsCancelled, 0)
                .one();

        if (like == null) {
            throw new ClientException("未点赞过");
        }

        // 取消点赞
        like.setIsCancelled(1);
        like.setUpdateTime(new Date());
        this.updateById(like);

        // TODO: 更新文章或评论的点赞数

        return true;
    }

    @Override
    public Boolean isLiked(Long userId, Long targetId, Integer targetType) {
        Like like = this.lambdaQuery()
                .eq(Like::getUserId, userId)
                .eq(Like::getTargetId, targetId)
                .eq(Like::getTargetType, targetType)
                .eq(Like::getIsCancelled, 0)
                .one();
        return like != null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean toggleLike(LikeRequestDTO request) {
        if (isLiked(ContextHolderUtils.getCurrentUserId(), request.getTargetId(), request.getTargetType())) {
            return unlike(request);
        } else {
            return like(request);
        }
    }
}
