package online.wesal.wesal.repository;

import online.wesal.wesal.entity.PostLike;
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.List;
import java.util.Optional;
import java.util.Set;

@Repository
public interface PostLikeRepository extends JpaRepository<PostLike, Long> {
    Optional<PostLike> findByPostIdAndUserId(Long postId, Long userId);
    boolean existsByPostIdAndUserId(Long postId, Long userId);
    void deleteByPostIdAndUserId(Long postId, Long userId);
    long countByPostId(Long postId);
    
    @Query("SELECT pl.postId FROM PostLike pl WHERE pl.userId = :userId AND pl.postId IN :postIds")
    Set<Long> findLikedPostIdsByUserIdAndPostIds(@Param("userId") Long userId, @Param("postIds") List<Long> postIds);
    
    @Query("SELECT pl.postId FROM PostLike pl WHERE pl.userId = :userId")
    Set<Long> findAllLikedPostIdsByUserId(@Param("userId") Long userId);
    
    @Query("SELECT pl FROM PostLike pl WHERE pl.postId = :postId ORDER BY pl.createdAt DESC")
    List<PostLike> findByPostIdOrderByCreatedAtDesc(@Param("postId") Long postId);
}