package ru.soune.nocopy.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import ru.soune.nocopy.entity.user.AuthToken;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Repository
public interface AuthTokenRepository extends JpaRepository<AuthToken, Long> {
    List<AuthToken> findByExpiresAtBefore(LocalDateTime expiresAtBefore);
    Optional<AuthToken> findByLastUsedAtBefore(LocalDateTime lastUsedAt);
    Optional<AuthToken> findByToken(String token);
    @Query(value = """
                    SELECT a.user_id FROM auth_tokens a WHERE a.token = :token
                   """,
            nativeQuery = true)
    Long findUserIdByToken(String token);
    Optional<AuthToken> findByUserIdAndIsActive(Long userId, boolean isActive);
    List<AuthToken> findByUserId(Long userId);
}
