package de.oaa.xxx.hashtag;

import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

public interface PostHashtagRepository extends JpaRepository<PostHashtagEntity, UUID> {

    List<PostHashtagEntity> findByHashtagId(UUID hashtagId);

    void deleteByPostTypeAndPostId(String postType, UUID postId);

    interface HashtagFrequency {
        UUID getHashtagId();
        Long getCnt();
    }

    @Query("SELECT ph.hashtagId AS hashtagId, COUNT(ph.id) AS cnt " +
           "FROM PostHashtagEntity ph " +
           "WHERE ph.createdAt >= :since " +
           "GROUP BY ph.hashtagId " +
           "ORDER BY cnt DESC")
    List<HashtagFrequency> findPopularSince(@Param("since") LocalDateTime since, Pageable pageable);
}
