package de.oaa.xxx.aufgaben;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.oaa.xxx.framework.AbstractDAO;
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;

public class FavoritDAO extends AbstractDAO<FavoritEntity> {

	private Logger LOGGER = LoggerFactory.getLogger(getClass());
	
	public FavoritDAO(EntityManager entityManager) {
		super(FavoritEntity.class, entityManager);
	}

	public List<FavoritEntity> getUserFavoriten(UUID userId) {
		try {
			TypedQuery<FavoritEntity> query = getEntityManager()
					.createQuery("select f from FavoritEntity f where f.userId = :userId", FavoritEntity.class);
			query.setParameter("userId", userId);
			return query.getResultList();
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			return Collections.emptyList();
		}
	}
	
	public FavoritEntity findByUserAndGruppe(UUID userId, UUID gruppeId) {
		TypedQuery<FavoritEntity> query = getEntityManager().createQuery("""
				select f from FavoritEntity f
					where f.userId = :userId
					and f.aufgabenGruppeId = :aufgabenGruppeId
				""",FavoritEntity.class);
		query.setParameter("userId", userId);
		query.setParameter("aufgabenGruppeId", gruppeId);
		if (query.getResultList().size() == 1) {
			return query.getSingleResult();
		}
		return null;
	}
	
	public List<FavoritEntity> findAllByUserAndGruppe(UUID userId, UUID gruppeId) {
		TypedQuery<FavoritEntity> query = getEntityManager().createQuery("""
				select f from FavoritEntity f
					where f.userId = :userId
					and f.aufgabenGruppeId = :aufgabenGruppeId
				""",FavoritEntity.class);
		query.setParameter("userId", userId);
		query.setParameter("aufgabenGruppeId", gruppeId);
		return query.getResultList();
	}
}
