package com.alterdekim.game.repository;

import com.alterdekim.game.entity.Miniquest;
import com.alterdekim.game.entity.PhoneMessage;
import jakarta.transaction.Transactional;
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.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface PhoneMessageRepository extends JpaRepository<PhoneMessage, Long> {
    Optional<PhoneMessage> findById(Long id);

    @Query(value = "SELECT p FROM PhoneMessage p WHERE p.receiverId = :uid")
    List<PhoneMessage> findByReceiverId(@Param("uid") Integer id);

    List<PhoneMessage> findAll();

    @Transactional
    @Modifying
    @Query("UPDATE PhoneMessage p SET p.isNew = false WHERE p.id = :mid")
    void markAsRead(@Param("mid") Long messageId);
}