package fr.titionfire.ffsaf.data.repository;

import fr.titionfire.ffsaf.data.model.TreeModel;
import io.quarkus.hibernate.reactive.panache.PanacheRepositoryBase;
import io.quarkus.hibernate.reactive.panache.common.WithTransaction;
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class TreeRepository implements PanacheRepositoryBase<TreeModel, Long> {

    @WithTransaction
    public Uni<Boolean> deleteTree(TreeModel entity) {
        Uni<Boolean> uni = Uni.createFrom().item(false);
        if (entity == null)
            return uni;
        if (entity.getLeft() != null)
            uni = uni.chain(__ -> this.deleteTree(entity.getLeft()));
        if (entity.getRight() != null)
            uni = uni.chain(__ -> this.deleteTree(entity.getRight()));
        return uni.chain(__ -> this.deleteById(entity.getId()));
    }
}
