package online.wesal.wesal.repository;

import online.wesal.wesal.entity.Comment;
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;

@Repository
public interface CommentRepository extends JpaRepository<Comment, Long> {
    
    List<Comment> findByPostIdOrderByCreationDateDesc(Long postId);
    
    @Query("SELECT c FROM Comment c WHERE c.postId = :postId ORDER BY c.creationDate ASC")
    List<Comment> findByPostIdOrderByCreationDateAsc(@Param("postId") Long postId);
    
    Optional<Comment> findByIdAndPostId(Long id, Long postId);
    
    long countByPostId(Long postId);
    
    @Query("SELECT c FROM Comment c WHERE c.replyComment = :replyComment ORDER BY c.creationDate ASC")
    List<Comment> findRepliesByReplyComment(@Param("replyComment") Long replyComment);
}