package com.gnx.telemedicine.repository;

import com.gnx.telemedicine.model.MedicalRecord;
import com.gnx.telemedicine.model.enums.RecordType;
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 MedicalRecordRepository extends JpaRepository<MedicalRecord, UUID> {
    
    List<MedicalRecord> findByPatientIdOrderByCreatedAtDesc(UUID patientId);
    
    List<MedicalRecord> findByPatientIdAndRecordTypeOrderByCreatedAtDesc(UUID patientId, RecordType recordType);
    
    List<MedicalRecord> findByDoctorIdOrderByCreatedAtDesc(UUID doctorId);
    
    List<MedicalRecord> findByAppointmentId(UUID appointmentId);
    
    @Query("SELECT mr FROM MedicalRecord mr WHERE mr.patient.id = :patientId AND mr.doctor.id = :doctorId ORDER BY mr.createdAt DESC")
    List<MedicalRecord> findByPatientIdAndDoctorId(@Param("patientId") UUID patientId, @Param("doctorId") UUID doctorId);
    
    @Query("SELECT COUNT(mr) FROM MedicalRecord mr WHERE mr.patient.id = :patientId AND mr.recordType = :recordType")
    Long countByPatientIdAndRecordType(@Param("patientId") UUID patientId, @Param("recordType") RecordType recordType);
}

