package com.gnx.telemedicine.repository;

import com.gnx.telemedicine.model.PhiAccessLog;
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 PhiAccessLogRepository extends JpaRepository<PhiAccessLog, UUID> {
    
    List<PhiAccessLog> findByPatientIdOrderByTimestampDesc(UUID patientId);
    
    List<PhiAccessLog> findByUserIdOrderByTimestampDesc(UUID userId);
    
    @Query("SELECT pal FROM PhiAccessLog pal WHERE pal.patient.id = :patientId AND pal.timestamp >= :startDate AND pal.timestamp <= :endDate ORDER BY pal.timestamp DESC")
    List<PhiAccessLog> findByPatientIdAndDateRange(@Param("patientId") UUID patientId, @Param("startDate") Instant startDate, @Param("endDate") Instant endDate);
    
    @Query("SELECT COUNT(DISTINCT pal.user.id) FROM PhiAccessLog pal WHERE pal.patient.id = :patientId AND pal.timestamp >= :startDate")
    Long countDistinctUsersAccessingPatient(@Param("patientId") UUID patientId, @Param("startDate") Instant startDate);
}

