package net.shyshkin.study.fullstack.supportportal.backend.repository;

import java.util.List;

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 net.shyshkin.study.fullstack.supportportal.backend.domain.Post;

@Repository
public interface PostRepository extends JpaRepository<Post, Long> {

    // Additional query methods can be defined here if needed

    // 1. Find all posts where isPosted is true
    List<Post> findAllByIsPostedTrue();

    // 3. Find all posts associated with a specific tag where isPosted is true
    @Query("SELECT p FROM Post p JOIN p.tags t WHERE t = :tag AND p.isPosted = true")
    List<Post> findAllByTagAndIsPostedTrue(@Param("tag") String tag);

    // Custom query to count unique tags
    @Query("SELECT t, COUNT(t) FROM Post p JOIN p.tags t GROUP BY t")
    List<Object[]> findTagsWithCount();

}