package com.gnx.telemedicine.repository;

import com.gnx.telemedicine.model.MedicationIntakeLog;
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 MedicationIntakeLogRepository extends JpaRepository<MedicationIntakeLog, UUID> {
    
    List<MedicationIntakeLog> findByPrescriptionIdOrderByScheduledTimeDesc(UUID prescriptionId);
    
    @Query("SELECT mil FROM MedicationIntakeLog mil WHERE mil.prescription.id = :prescriptionId AND mil.taken = false AND mil.scheduledTime <= :currentTime ORDER BY mil.scheduledTime ASC")
    List<MedicationIntakeLog> findMissedDoses(@Param("prescriptionId") UUID prescriptionId, @Param("currentTime") Instant currentTime);
    
    @Query("SELECT COUNT(mil) FROM MedicationIntakeLog mil WHERE mil.prescription.id = :prescriptionId AND mil.taken = true")
    Long countTakenDosesByPrescriptionId(@Param("prescriptionId") UUID prescriptionId);
    
    @Query("SELECT COUNT(mil) FROM MedicationIntakeLog mil WHERE mil.prescription.id = :prescriptionId")
    Long countTotalDosesByPrescriptionId(@Param("prescriptionId") UUID prescriptionId);
}

