package de.oaa.xxx.social.repository;

import de.oaa.xxx.social.entity.BlockEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;
import java.util.UUID;

public interface BlockRepository extends JpaRepository<BlockEntity, UUID> {

    Optional<BlockEntity> findByBlockerIdAndBlockedId(UUID blockerId, UUID blockedId);

    /** True if either user has blocked the other. */
    @Query("SELECT COUNT(b) > 0 FROM BlockEntity b WHERE (b.blockerId = :a AND b.blockedId = :b) OR (b.blockerId = :b AND b.blockedId = :a)")
    boolean existsBlock(@Param("a") UUID a, @Param("b") UUID b);

    @Modifying
    @Transactional
    @Query("DELETE FROM BlockEntity b WHERE b.blockerId = :blockerId AND b.blockedId = :blockedId")
    void deleteByBlockerIdAndBlockedId(@Param("blockerId") UUID blockerId, @Param("blockedId") UUID blockedId);
}
