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

import org.springframework.data.domain.Page;
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.ToyEntity;

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

public interface ToyRepository extends JpaRepository<ToyEntity, UUID> {

    Page<ToyEntity> findByUserIdIsNull(Pageable pageable);

    Page<ToyEntity> findByUserId(UUID userId, Pageable pageable);
    long countByUserId(UUID userId);

    List<ToyEntity> findByUserId(UUID userId);

    boolean existsByNameIgnoreCaseAndUserIdIsNull(String name);

    boolean existsByNameIgnoreCaseAndUserId(String name, UUID userId);

    boolean existsByNameIgnoreCaseAndUserIdIsNullAndToyIdNot(String name, UUID toyId);

    boolean existsByNameIgnoreCaseAndUserIdAndToyIdNot(String name, UUID userId, UUID toyId);

    Optional<ToyEntity> findByNameIgnoreCaseAndUserId(String name, UUID userId);

    @Query("SELECT COUNT(a) FROM AufgabeEntity a JOIN a.benoetigteToys t WHERE t.toyId = :toyId")
    long countAufgabeUsage(@Param("toyId") UUID toyId);

    @Query("SELECT COUNT(s) FROM StrafeEntity s JOIN s.benoetigteToys t WHERE t.toyId = :toyId")
    long countStrafeUsage(@Param("toyId") UUID toyId);

    @Query("SELECT COUNT(sp) FROM SperreEntity sp JOIN sp.benoetigteToys t WHERE t.toyId = :toyId")
    long countSperreUsage(@Param("toyId") UUID toyId);
}
