package com.yaoyuan.jiscuss.repository;

import com.yaoyuan.jiscuss.entity.LikeCollect;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface LikeCollectRepository extends JpaRepository<LikeCollect, Integer> {

    /** Find an existing vote record for a user on a discussion. */
    @Query("FROM LikeCollect lc WHERE lc.userId = :userId AND lc.discussionId = :discussionId AND lc.type = 'discussion'")
    Optional<LikeCollect> findDiscussionVote(@Param("userId") Integer userId, @Param("discussionId") Integer discussionId);

    /** Find an existing vote record for a user on a post. */
    @Query("FROM LikeCollect lc WHERE lc.userId = :userId AND lc.postId = :postId AND lc.type = 'post'")
    Optional<LikeCollect> findPostVote(@Param("userId") Integer userId, @Param("postId") Integer postId);

    /** Discussions liked by a user (action = 'like'). */
    @Query("SELECT lc.discussionId FROM LikeCollect lc WHERE lc.userId = :userId AND lc.type = 'discussion' AND lc.action = 'like'")
    java.util.List<Integer> findLikedDiscussionIdsByUser(@Param("userId") Integer userId);
}
