package com.gnx.telemedicine.repository;

import com.gnx.telemedicine.model.DuplicatePatientRecord;
import com.gnx.telemedicine.model.Patient;
import com.gnx.telemedicine.model.enums.DuplicateStatus;
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.Optional;
import java.util.UUID;

@Repository
public interface DuplicatePatientRecordRepository extends JpaRepository<DuplicatePatientRecord, UUID> {
    List<DuplicatePatientRecord> findByPrimaryPatientOrderByCreatedAtDesc(Patient patient);
    
    List<DuplicatePatientRecord> findByDuplicatePatientOrderByCreatedAtDesc(Patient patient);
    
    List<DuplicatePatientRecord> findByStatusOrderByCreatedAtDesc(DuplicateStatus status);
    
    Optional<DuplicatePatientRecord> findByPrimaryPatientAndDuplicatePatient(Patient primary, Patient duplicate);
    
    @Query("SELECT COUNT(d) FROM DuplicatePatientRecord d WHERE d.status = 'PENDING'")
    long countPendingDuplicates();
    
    @Query("SELECT d FROM DuplicatePatientRecord d WHERE d.status = 'PENDING' ORDER BY d.matchScore DESC, d.createdAt DESC")
    List<DuplicatePatientRecord> findAllPendingOrderedByMatchScore();
}

