package cz.moneta.test.harness.endpoints.cagw;

import cz.moneta.test.harness.connectors.rest.RestConnector;
import cz.moneta.test.harness.connectors.rest.SimpleRestConnector;
import cz.moneta.test.harness.constants.HarnessConfigConstants;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.RestEndpoint;
import cz.moneta.test.harness.support.auth.AuthSupport;
import org.apache.commons.lang3.tuple.Pair;

import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.GenericType;
import java.util.Map;

public class CaGwEndpoint implements RestEndpoint {

    private RestConnector restConnector;
    private StoreAccessor store;

    public CaGwEndpoint(StoreAccessor store) {
        this.store = store;
        String url = store.getConfig("endpoints.cagw.url");
        if (url == null) {
            throw new IllegalStateException("You need to configure endpoints.cagw.url to work with CBL");
        }
        this.restConnector = new SimpleRestConnector(url, "CaGwRestLogger");

    }

    @Override
    public <T> Pair<Integer, T> get(String path, Map<String, Object> properties, Class<T> responseType, Map<String, Object> headers) {
        return restConnector.get(path, properties, new GenericType<>(responseType), headers);
    }

    @Override
    public <T> Pair<Integer, T> post(String path, Entity<?> request, Class<T> responseType, Map<String, Object> headers, boolean rawResponse) {
        return restConnector.post(path, request, new GenericType<>(responseType), headers);
    }

    @Override
    public <T> Pair<Integer, T> delete(String path, Map<String, Object> properties, Class<T> responseType, Map<String, Object> headers) {
        return restConnector.delete(path, properties, new GenericType<>(responseType), headers);
    }

    @Override
    public <T> Pair<Integer, T> patch(String path, Entity<?> request, Class<T> responseType, Map<String, Object> headers, boolean rawResponse) {
        return restConnector.patch(path, request, new GenericType<>(responseType), headers);
    }

    public String getClientSecret(String clientKey) {
        return AuthSupport.getClientSecret(clientKey, store, HarnessConfigConstants.VAULT_CAGW_KEYS_PATH);
    }

    @Override
    public StoreAccessor getStore() {
        return store;
    }

}
