package de.votian.services.repository;

import de.votian.services.entity.GroupwareAppointment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

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

public interface GroupwareAppointmentRepository extends JpaRepository<GroupwareAppointment, Long> {

    @Query("SELECT ap FROM GroupwareAppointment ap " +
           "WHERE ap.headquartersId = :hqId " +
           "AND ap.execDate <= :rangeEnd " +
           "AND (ap.endDate IS NULL OR ap.endDate >= :rangeStart) " +
           "ORDER BY ap.execDate ASC, ap.id ASC")
    List<GroupwareAppointment> findOverlapping(@Param("hqId") Long headquartersId,
                                               @Param("rangeStart") LocalDateTime rangeStart,
                                               @Param("rangeEnd") LocalDateTime rangeEnd);

    Optional<GroupwareAppointment> findByIdAndHeadquartersId(Long id, Long headquartersId);
}
