package de.cannamanage.service.repository;

import de.cannamanage.domain.entity.InfoBoardPost;
import de.cannamanage.domain.enums.InfoBoardCategory;
import org.springframework.data.domain.Page;
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 org.springframework.stereotype.Repository;

import java.util.UUID;

@Repository
public interface InfoBoardPostRepository extends JpaRepository<InfoBoardPost, UUID> {

    Page<InfoBoardPost> findByClubIdAndArchivedFalseOrderByPinnedDescCreatedAtDesc(
            UUID clubId, Pageable pageable);

    Page<InfoBoardPost> findByClubIdAndCategoryAndArchivedFalseOrderByPinnedDescCreatedAtDesc(
            UUID clubId, InfoBoardCategory category, Pageable pageable);

    Page<InfoBoardPost> findByClubIdOrderByPinnedDescCreatedAtDesc(
            UUID clubId, Pageable pageable);

    Page<InfoBoardPost> findByClubIdAndCategoryOrderByPinnedDescCreatedAtDesc(
            UUID clubId, InfoBoardCategory category, Pageable pageable);

    @Query("SELECT COUNT(p) FROM InfoBoardPost p WHERE p.clubId = :clubId AND p.archived = false " +
           "AND p.id NOT IN (SELECT r.postId FROM PostReadStatus r WHERE r.memberId = :memberId)")
    long countUnreadByClubIdAndMemberId(@Param("clubId") UUID clubId, @Param("memberId") UUID memberId);
}
