package online.wesal.wesal.repository;

import online.wesal.wesal.entity.Invitation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface InvitationRepository extends JpaRepository<Invitation, Long> {
    
    @Query("SELECT i FROM Invitation i WHERE i.currentAttendees < i.maxParticipants AND (i.dateTime IS NULL OR i.dateTime > CURRENT_TIMESTAMP) ORDER BY i.id ASC")
    List<Invitation> findAvailableInvitationsOrderByCreationDate();
    
    @Query("SELECT i FROM Invitation i WHERE i.creator.id = :userId ORDER BY i.id DESC")
    List<Invitation> findByCreatorIdOrderByCreationDateDesc(Long userId);
    
    @Query("SELECT i FROM Invitation i JOIN Attendee a ON a.invitation.id = i.id WHERE a.user.id = :userId ORDER BY i.id DESC")
    List<Invitation> findAcceptedInvitationsByUserIdOrderByCreationDateDesc(Long userId);
}