package com.gnx.telemedicine.repository;

import com.gnx.telemedicine.model.SentinelEvent;
import com.gnx.telemedicine.model.enums.SentinelEventStatus;
import com.gnx.telemedicine.model.enums.SentinelEventType;
import com.gnx.telemedicine.model.enums.SentinelSeverity;
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 SentinelEventRepository extends JpaRepository<SentinelEvent, UUID> {
    List<SentinelEvent> findByStatusOrderByOccurredAtDesc(SentinelEventStatus status);
    
    List<SentinelEvent> findByEventTypeOrderByOccurredAtDesc(SentinelEventType eventType);
    
    List<SentinelEvent> findBySeverityOrderByOccurredAtDesc(SentinelSeverity severity);
    
    @Query("SELECT COUNT(s) FROM SentinelEvent s WHERE s.status = 'REPORTED' OR s.status = 'UNDER_INVESTIGATION'")
    long countActiveIncidents();
    
    @Query("SELECT s FROM SentinelEvent s WHERE s.status IN ('REPORTED', 'UNDER_INVESTIGATION') ORDER BY s.occurredAt DESC")
    List<SentinelEvent> findAllActiveIncidents();
    
    @Query("SELECT s FROM SentinelEvent s WHERE s.occurredAt BETWEEN :startDate AND :endDate ORDER BY s.occurredAt DESC")
    List<SentinelEvent> findByOccurredAtBetween(@Param("startDate") Instant startDate, @Param("endDate") Instant endDate);
}

