package de.oaa.xxx.games.common.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
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 de.oaa.xxx.games.common.entity.AufgabenGruppeEntity;

import java.util.List;
import java.util.UUID;

public interface AufgabenGruppeRepository extends JpaRepository<AufgabenGruppeEntity, UUID> {

    @Query("select age from AufgabenGruppeEntity age where age.userId = :userId")
    List<AufgabenGruppeEntity> findByUserId(@Param("userId") UUID userId);

    long countByUserId(UUID userId);

    Page<AufgabenGruppeEntity> findByUserIdIsNull(Pageable pageable);

    Page<AufgabenGruppeEntity> findByUserId(UUID userId, Pageable pageable);

    @Query("select age from AufgabenGruppeEntity age where (age.privateGruppe = false or age.userId = :userId) and (:search is null or age.name like :search)")
    List<AufgabenGruppeEntity> listWithUserAndSearch(@Param("userId") UUID userId, @Param("search") String search, PageRequest pageable);

    @Query("select age from AufgabenGruppeEntity age where age.privateGruppe = false and (:search is null or age.name like :search)")
    List<AufgabenGruppeEntity> listPublicWithSearch(@Param("search") String search, PageRequest pageable);

    @Query("select age from AufgabenGruppeEntity age where age.privateGruppe = false and age.userId is not null and age.userId <> :userId and (:name is null or lower(age.name) like lower(:name))")
    List<AufgabenGruppeEntity> findPublicFromOthers(@Param("userId") UUID userId, @Param("name") String name);

    @Query("SELECT g FROM AufgabenGruppeEntity g WHERE (g.privateGruppe = false OR g.userId = :userId) AND g.strafen IS EMPTY AND g.sperren IS EMPTY AND (:search IS NULL OR g.name LIKE :search)")
    List<AufgabenGruppeEntity> listVanillaSafeWithUserAndSearch(@Param("userId") UUID userId, @Param("search") String search, PageRequest pageable);

    @Query("SELECT g FROM AufgabenGruppeEntity g WHERE g.privateGruppe = false AND g.userId IS NOT NULL AND g.userId <> :userId AND g.strafen IS EMPTY AND g.sperren IS EMPTY AND (:name IS NULL OR LOWER(g.name) LIKE LOWER(:name))")
    List<AufgabenGruppeEntity> findVanillaSafePublicFromOthers(@Param("userId") UUID userId, @Param("name") String name);
}
