package com.magistr.app.repository;

import com.magistr.app.model.TimeSlotDateAssignment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

public interface TimeSlotDateAssignmentRepository extends JpaRepository<TimeSlotDateAssignment, Long> {

    Optional<TimeSlotDateAssignment> findByDate(LocalDate date);

    List<TimeSlotDateAssignment> findAllByOrderByDateAsc();

    List<TimeSlotDateAssignment> findByDateBetweenOrderByDateAsc(LocalDate startDate, LocalDate endDate);

    @Query("""
            select assignment
            from TimeSlotDateAssignment assignment
            join fetch assignment.timeSlotScope
            where assignment.date between :startDate and :endDate
            order by assignment.date
            """)
    List<TimeSlotDateAssignment> findForScheduleRange(@Param("startDate") LocalDate startDate,
                                                      @Param("endDate") LocalDate endDate);

    boolean existsByTimeSlotScopeId(Long scopeId);
}
