package com.gnx.telemedicine.repository;

import com.gnx.telemedicine.model.ClinicalAlert;
import com.gnx.telemedicine.model.Patient;
import com.gnx.telemedicine.model.enums.AlertSeverity;
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.util.List;
import java.util.UUID;

@Repository
public interface ClinicalAlertRepository extends JpaRepository<ClinicalAlert, UUID> {
    List<ClinicalAlert> findByPatientOrderByCreatedAtDesc(Patient patient);
    
    List<ClinicalAlert> findByPatientAndAcknowledgedFalseOrderBySeverityDescCreatedAtDesc(Patient patient);
    
    List<ClinicalAlert> findByAcknowledgedFalseOrderBySeverityDescCreatedAtDesc();
    
    long countByPatientAndAcknowledgedFalse(Patient patient);
    
    List<ClinicalAlert> findBySeverityAndAcknowledgedFalseOrderByCreatedAtDesc(AlertSeverity severity);
    
    @Query("SELECT ca FROM ClinicalAlert ca WHERE ca.patient.id = :patientId AND ca.acknowledged = false ORDER BY ca.severity DESC, ca.createdAt DESC")
    List<ClinicalAlert> findUnacknowledgedAlertsByPatientId(UUID patientId);
    
    // Find alerts for a specific doctor (alerts assigned to this doctor)
    @Query("SELECT ca FROM ClinicalAlert ca WHERE ca.doctor.id = :doctorId AND ca.acknowledged = false ORDER BY ca.severity DESC, ca.createdAt DESC")
    List<ClinicalAlert> findUnacknowledgedAlertsByDoctorId(UUID doctorId);
    
    // Find alerts for a specific doctor and patient
    @Query("SELECT ca FROM ClinicalAlert ca WHERE ca.doctor.id = :doctorId AND ca.patient.id = :patientId ORDER BY ca.severity DESC, ca.createdAt DESC")
    List<ClinicalAlert> findByDoctorIdAndPatientId(@Param("doctorId") UUID doctorId, @Param("patientId") UUID patientId);
    
    // Find unacknowledged alerts for a specific doctor and patient
    @Query("SELECT ca FROM ClinicalAlert ca WHERE ca.doctor.id = :doctorId AND ca.patient.id = :patientId AND ca.acknowledged = false ORDER BY ca.severity DESC, ca.createdAt DESC")
    List<ClinicalAlert> findUnacknowledgedAlertsByDoctorIdAndPatientId(@Param("doctorId") UUID doctorId, @Param("patientId") UUID patientId);
    
    // Check for duplicate unacknowledged drug interaction alerts
    @Query("SELECT ca FROM ClinicalAlert ca WHERE ca.patient.id = :patientId AND ca.alertType = 'DRUG_INTERACTION' AND ca.acknowledged = false AND (ca.description LIKE CONCAT('%', :med1, '%') OR ca.description LIKE CONCAT('%', :med2, '%'))")
    List<ClinicalAlert> findDuplicateDrugInteractionAlerts(@Param("patientId") UUID patientId, @Param("med1") String medication1, @Param("med2") String medication2);
    
    // Check for duplicate unacknowledged allergy alerts
    @Query("SELECT ca FROM ClinicalAlert ca WHERE ca.patient.id = :patientId AND ca.alertType = 'ALLERGY' AND ca.acknowledged = false AND ca.medicationName = :medicationName")
    List<ClinicalAlert> findDuplicateAllergyAlerts(@Param("patientId") UUID patientId, @Param("medicationName") String medicationName);
}

