package com.izouma.nineth.repo;

import com.izouma.nineth.domain.IdentityAuth;
import com.izouma.nineth.enums.AuthStatus;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

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

public interface IdentityAuthRepo extends JpaRepository<IdentityAuth, Long>, JpaSpecificationExecutor<IdentityAuth> {
    @Query("update IdentityAuth t set t.del = true where t.id = ?1")
    @Modifying
    @Transactional
    void softDelete(Long id);

    List<IdentityAuth> findByUserIdAndDelFalse(Long userId);

    Optional<IdentityAuth> findFirstByUserIdAndStatusAndDelFalseOrderByCreatedAtDesc(Long userId, AuthStatus status);

    Optional<IdentityAuth> findByIdAndDelFalse(Long id);

    @Query(value = "select t.id from IdentityAuth t where t.status = 'PENDING' and t.del =false")
    List<Long> findAllPendingId();

    @Query(nativeQuery = true,value ="select count(*) from identity_auth  where id_no =?1 and status = 'SUCCESS' and del = false")
    Integer findByIdNoAndAuthSuccess(String idNo);
}