package com.yaoyuan.jiscuss.service.impl;

import com.yaoyuan.jiscuss.entity.LikeCollect;
import com.yaoyuan.jiscuss.entity.Post;
import com.yaoyuan.jiscuss.repository.DiscussionsRepository;
import com.yaoyuan.jiscuss.repository.LikeCollectRepository;
import com.yaoyuan.jiscuss.repository.PostsRepository;
import com.yaoyuan.jiscuss.service.ILikeCollectService;
import com.yaoyuan.jiscuss.service.INotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Optional;

@Service
public class LikeCollectServiceImpl implements ILikeCollectService {

    @Autowired
    private LikeCollectRepository likeCollectRepository;

    @Autowired
    private DiscussionsRepository discussionsRepository;

    @Autowired
    private PostsRepository postsRepository;

    @Autowired
    private INotificationService notificationService;

    @Transactional
    @Override
    public String voteDiscussion(Integer userId, Integer discussionId, String action) {
        Optional<LikeCollect> existing = likeCollectRepository.findDiscussionVote(userId, discussionId);
        if (existing.isPresent()) {
            LikeCollect vote = existing.get();
            if (vote.getAction().equals(action)) {
                // Toggle off: cancel the same action
                likeCollectRepository.delete(vote);
                adjustDiscussion(discussionId, action, -1);
                return "none";
            } else {
                // Switch action: undo old, apply new
                adjustDiscussion(discussionId, vote.getAction(), -1);
                vote.setAction(action);
                likeCollectRepository.save(vote);
                adjustDiscussion(discussionId, action, +1);
                return action;
            }
        } else {
            // New vote
            LikeCollect vote = new LikeCollect();
            vote.setUserId(userId);
            vote.setDiscussionId(discussionId);
            vote.setType("discussion");
            vote.setAction(action);
            vote.setCreateId(userId);
            vote.setCreateTime(new Date());
            likeCollectRepository.save(vote);
            adjustDiscussion(discussionId, action, +1);
            return action;
        }
    }

    @Transactional
    @Override
    public String votePost(Integer userId, Integer postId, String action) {
        Optional<LikeCollect> existing = likeCollectRepository.findPostVote(userId, postId);
        if (existing.isPresent()) {
            LikeCollect vote = existing.get();
            if (vote.getAction().equals(action)) {
                likeCollectRepository.delete(vote);
                adjustPost(postId, action, -1);
                return "none";
            } else {
                adjustPost(postId, vote.getAction(), -1);
                vote.setAction(action);
                likeCollectRepository.save(vote);
                adjustPost(postId, action, +1);
                return action;
            }
        } else {
            LikeCollect vote = new LikeCollect();
            vote.setUserId(userId);
            vote.setPostId(postId);
            vote.setType("post");
            vote.setAction(action);
            vote.setCreateId(userId);
            vote.setCreateTime(new Date());
            likeCollectRepository.save(vote);
            adjustPost(postId, action, +1);
            // Notify post author when liked (not disliked)
            if ("like".equals(action)) {
                Post post = postsRepository.findById(postId).orElse(null);
                if (post != null && post.getCreateId() != null) {
                    notificationService.notifyLike(post.getCreateId(), userId, postId, post.getDiscussionId());
                }
            }
            return action;
        }
    }

    private void adjustDiscussion(Integer id, String action, int delta) {
        if ("like".equals(action)) {
            discussionsRepository.adjustLikeCount(id, delta);
        } else {
            discussionsRepository.adjustDislikeCount(id, delta);
        }
    }

    private void adjustPost(Integer id, String action, int delta) {
        if ("like".equals(action)) {
            postsRepository.adjustLikeCount(id, delta);
        } else {
            postsRepository.adjustDislikeCount(id, delta);
        }
    }
}
