package com.gnx.telemedicine.repository;

import com.gnx.telemedicine.model.LoginAttempt;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.Instant;
import java.util.List;
import java.util.UUID;

@Repository
public interface LoginAttemptRepository extends JpaRepository<LoginAttempt, UUID> {
    
    @Query("SELECT la FROM LoginAttempt la WHERE la.email = :email AND la.timestamp >= :since ORDER BY la.timestamp DESC")
    List<LoginAttempt> findRecentAttemptsByEmail(@Param("email") String email, @Param("since") Instant since);
    
    @Query("SELECT COUNT(la) FROM LoginAttempt la WHERE la.email = :email AND la.success = false AND la.timestamp >= :since")
    Long countFailedAttemptsSince(@Param("email") String email, @Param("since") Instant since);
    
    @Query("SELECT la FROM LoginAttempt la WHERE la.ipAddress = :ipAddress AND la.timestamp >= :since ORDER BY la.timestamp DESC")
    List<LoginAttempt> findRecentAttemptsByIpAddress(@Param("ipAddress") String ipAddress, @Param("since") Instant since);
}

