package de.oaa.xxx.location.repository;

import de.oaa.xxx.location.entity.LocationEventEntity;
import org.springframework.data.domain.Pageable;
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.Collection;
import java.util.List;
import java.util.UUID;

public interface LocationEventRepository extends JpaRepository<LocationEventEntity, UUID> {

    List<LocationEventEntity> findByLocationIdOrderByStartAtAsc(UUID locationId);

    @Query("SELECT e FROM LocationEventEntity e WHERE e.locationId IN :locationIds AND e.startAt >= :from ORDER BY e.startAt ASC")
    List<LocationEventEntity> findUpcomingByLocationIds(@Param("locationIds") Collection<UUID> locationIds, @Param("from") LocalDateTime from);

    @Query("SELECT e FROM LocationEventEntity e WHERE e.eventId IN :eventIds AND e.startAt >= :from ORDER BY e.startAt ASC")
    List<LocationEventEntity> findUpcomingByEventIds(@Param("eventIds") Collection<UUID> eventIds, @Param("from") LocalDateTime from);

    /** Alle zukünftigen Events mit Koordinaten ihrer Location (für Umkreis-Suche) */
    @Query("""
            SELECT e FROM LocationEventEntity e
             WHERE e.startAt >= :from
               AND e.startAt <= :to
            """)
    List<LocationEventEntity> findInTimeRange(LocalDateTime from, LocalDateTime to);

    @Query("SELECT e FROM LocationEventEntity e WHERE LOWER(e.title) LIKE LOWER(CONCAT('%', :title, '%')) ORDER BY e.startAt ASC")
    List<LocationEventEntity> findByTitleContainingIgnoreCase(@Param("title") String title, Pageable pageable);

    void deleteByLocationId(UUID locationId);
}
