package com.izouma.uwip.repo;

import com.izouma.uwip.domain.SmsRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

public interface SmsRecordRepo extends JpaRepository<SmsRecord, Long> {
    Optional<SmsRecord> findLastBySessionIdAndExpiresAtAfterAndExpiredFalse(String sessionId, LocalDateTime time);

    Optional<SmsRecord> findLastByPhoneAndExpiresAtAfterAndExpiredFalse(String phone, LocalDateTime time);

    List<SmsRecord> findAllByPhoneAndExpiresAtAfterAndExpiredFalse(String phone, LocalDateTime time);

    @Query("update SmsRecord s set s.expired = true where s.phone = ?1")
    @Modifying
    @Transactional
    void expire(String phone);
}
