package com.gnx.telemedicine.repository;

import com.gnx.telemedicine.model.VitalSigns;
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.time.Instant;
import java.util.List;
import java.util.UUID;

@Repository
public interface VitalSignsRepository extends JpaRepository<VitalSigns, UUID> {
    
    List<VitalSigns> findByPatientIdOrderByRecordedAtDesc(UUID patientId);
    
    List<VitalSigns> findByAppointmentId(UUID appointmentId);
    
    @Query("SELECT vs FROM VitalSigns vs WHERE vs.patient.id = :patientId AND vs.recordedAt >= :startDate ORDER BY vs.recordedAt DESC")
    List<VitalSigns> findByPatientIdAndRecordedAtAfter(@Param("patientId") UUID patientId, @Param("startDate") Instant startDate);
    
    @Query(value = "SELECT * FROM vital_signs WHERE patient_id = :patientId ORDER BY recorded_at DESC LIMIT 1", nativeQuery = true)
    VitalSigns findLatestByPatientId(@Param("patientId") UUID patientId);
}

