package com.gnx.telemedicine.repository;

import com.gnx.telemedicine.model.LabResult;
import com.gnx.telemedicine.model.enums.LabResultStatus;
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 LabResultRepository extends JpaRepository<LabResult, UUID> {
    
    List<LabResult> findByPatientIdOrderByCreatedAtDesc(UUID patientId);
    
    List<LabResult> findByMedicalRecordId(UUID medicalRecordId);
    
    List<LabResult> findByPatientIdAndStatusOrderByCreatedAtDesc(UUID patientId, LabResultStatus status);
    
    @Query("SELECT lr FROM LabResult lr WHERE lr.patient.id = :patientId AND lr.status IN ('ABNORMAL', 'CRITICAL') ORDER BY lr.createdAt DESC")
    List<LabResult> findAbnormalResultsByPatientId(@Param("patientId") UUID patientId);
    
    @Query("SELECT COUNT(lr) FROM LabResult lr WHERE lr.patient.id = :patientId AND lr.status = :status")
    Long countByPatientIdAndStatus(@Param("patientId") UUID patientId, @Param("status") LabResultStatus status);
}

