package fr.titionfire.ffsaf.ws.recv;

import fr.titionfire.ffsaf.data.repository.CompetitionRepository;
import fr.titionfire.ffsaf.domain.entity.CombEntity;
import fr.titionfire.ffsaf.ws.PermLevel;
import io.quarkus.hibernate.reactive.panache.common.WithSession;
import io.quarkus.runtime.annotations.RegisterForReflection;
import io.quarkus.websockets.next.WebSocketConnection;
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.hibernate.reactive.mutiny.Mutiny;

import java.util.ArrayList;

@WithSession
@ApplicationScoped
@RegisterForReflection
public class RRegister {

    @Inject
    CompetitionRepository competitionRepository;

    @WSReceiver(code = "getRegister", permission = PermLevel.ADMIN)
    public Uni<?> getRegister(WebSocketConnection connection, Object o) {
        return competitionRepository.find("uuid", connection.pathParam("uuid")).firstResult()
                .call(cm -> Mutiny.fetch(cm.getInsc()))
                .call(cm -> Mutiny.fetch(cm.getGuests()))
                .map(cm -> {
                    ArrayList<CombEntity> combEntities = new ArrayList<>();
                    combEntities.addAll(cm.getInsc().stream().map(CombEntity::fromModel).toList());
                    combEntities.addAll(cm.getGuests().stream().map(CombEntity::fromModel).toList());
                    return combEntities;
                });
    }
}
