package com.saas.tenant.repository;

import com.saas.tenant.entity.Appointment;
import com.saas.tenant.entity.AppointmentStatus;
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.LocalDateTime;
import java.util.List;

@Repository
public interface AppointmentRepository extends JpaRepository<Appointment, Long> {

    List<Appointment> findByPatientId(Long patientId);

    List<Appointment> findByDoctorId(Long doctorId);

    List<Appointment> findByStatus(AppointmentStatus status);

    @Query("SELECT a FROM Appointment a WHERE a.patient.id = :patientId AND a.status = :status")
    List<Appointment> findByPatientIdAndStatus(@Param("patientId") Long patientId,
            @Param("status") AppointmentStatus status);

    @Query("SELECT a FROM Appointment a WHERE a.doctor.id = :doctorId AND a.status = :status")
    List<Appointment> findByDoctorIdAndStatus(@Param("doctorId") Long doctorId,
            @Param("status") AppointmentStatus status);

    @Query("SELECT a FROM Appointment a WHERE a.appointmentDateTime BETWEEN :startDate AND :endDate")
    List<Appointment> findByDateRange(@Param("startDate") LocalDateTime startDate,
            @Param("endDate") LocalDateTime endDate);

    @Query("SELECT a FROM Appointment a WHERE a.doctor.id = :doctorId " +
            "AND a.appointmentDateTime BETWEEN :startDate AND :endDate " +
            "AND a.status NOT IN ('CANCELED', 'NO_SHOW')")
    List<Appointment> findDoctorSchedule(
            @Param("doctorId") Long doctorId,
            @Param("startDate") LocalDateTime startDate,
            @Param("endDate") LocalDateTime endDate);

    @Query("SELECT a FROM Appointment a WHERE a.resource.id = :resourceId " +
            "AND a.appointmentDateTime BETWEEN :startDate AND :endDate " +
            "AND a.status NOT IN ('CANCELED', 'NO_SHOW')")
    List<Appointment> findResourceSchedule(
            @Param("resourceId") Long resourceId,
            @Param("startDate") LocalDateTime startDate,
            @Param("endDate") LocalDateTime endDate);

    @Query("SELECT a FROM Appointment a WHERE a.doctor.id = :doctorId " +
            "AND a.appointmentDateTime BETWEEN :startTime AND :endTime " +
            "AND a.status NOT IN ('CANCELED', 'NO_SHOW')")
    List<Appointment> findConflictingAppointments(
            @Param("doctorId") Long doctorId,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);

    @Query("SELECT a FROM Appointment a WHERE a.isAiScheduled = true")
    List<Appointment> findAiScheduledAppointments();

    @Query("SELECT a FROM Appointment a WHERE a.status = 'CONFIRMED' " +
            "AND a.reminderSent = false " +
            "AND a.appointmentDateTime BETWEEN :now AND :reminderWindow")
    List<Appointment> findAppointmentsNeedingReminder(
            @Param("now") LocalDateTime now,
            @Param("reminderWindow") LocalDateTime reminderWindow);
}
