package fr.titionfire.ffsaf.rest.client;

import fr.titionfire.ffsaf.domain.service.HelloAssoTokenService;
import io.quarkus.rest.client.reactive.ReactiveClientHeadersFactory;
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.MultivaluedMap;
import org.jboss.resteasy.reactive.common.util.MultivaluedTreeMap;

@ApplicationScoped
public class HelloAssoHeadersFactory extends ReactiveClientHeadersFactory {

    @Inject
    HelloAssoTokenService helloAssoTokenService;

    @Override
    public Uni<MultivaluedMap<String, String>> getHeaders(MultivaluedMap<String, String> incomingHeaders,
                                                          MultivaluedMap<String, String> clientOutgoingHeaders) {
        MultivaluedMap<String, String> map = new MultivaluedTreeMap<>();
        return helloAssoTokenService.getValidAccessToken()
                .invoke(token -> map.putSingle("Authorization", "Bearer " + token)).map(__ -> map);
    }
}
