package com.gnx.telemedicine.repository;

import com.gnx.telemedicine.model.CriticalResult;
import com.gnx.telemedicine.model.Doctor;
import com.gnx.telemedicine.model.Patient;
import com.gnx.telemedicine.model.enums.CriticalityLevel;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.UUID;

@Repository
public interface CriticalResultRepository extends JpaRepository<CriticalResult, UUID> {
    List<CriticalResult> findByPatientOrderByCreatedAtDesc(Patient patient);
    
    List<CriticalResult> findByDoctorOrderByCreatedAtDesc(Doctor doctor);
    
    List<CriticalResult> findByPatientAndAcknowledgedFalseOrderByCriticalityLevelDescCreatedAtDesc(Patient patient);
    
    List<CriticalResult> findByAcknowledgedFalseOrderByCriticalityLevelDescCreatedAtDesc();
    
    long countByPatientAndAcknowledgedFalse(Patient patient);
    
    long countByDoctorAndAcknowledgedFalse(Doctor doctor);
    
    List<CriticalResult> findByCriticalityLevelAndAcknowledgedFalseOrderByCreatedAtDesc(CriticalityLevel criticalityLevel);
    
    @Query("SELECT cr FROM CriticalResult cr WHERE cr.patient.id = :patientId AND cr.acknowledged = false ORDER BY cr.criticalityLevel DESC, cr.createdAt DESC")
    List<CriticalResult> findUnacknowledgedCriticalResultsByPatientId(UUID patientId);
    
    @Query("SELECT cr FROM CriticalResult cr WHERE cr.doctor.id = :doctorId AND cr.acknowledged = false ORDER BY cr.criticalityLevel DESC, cr.createdAt DESC")
    List<CriticalResult> findUnacknowledgedCriticalResultsByDoctorId(UUID doctorId);
}

