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

import cz.moneta.test.harness.connectors.rest.RestConnector;
import cz.moneta.test.harness.connectors.rest.SimpleRestConnector;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.RestEndpoint;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.GenericType;
import org.apache.commons.lang3.tuple.Pair;

import java.util.Map;
import java.util.Optional;

public class PSmartAutoEndpoint implements RestEndpoint {
    private RestConnector restConnector;
    private StoreAccessor store;

    public PSmartAutoEndpoint(StoreAccessor store) {
        this.store = store;
        String endpointName = "endpoints.psmartauto.url";
        this.restConnector = Optional.ofNullable(store.getConfig(endpointName))
                .map(url -> new SimpleRestConnector(url, "PSmartAutoRestLogger"))
                .orElseThrow(() -> new IllegalStateException("You need to configure " + endpointName + " to work with PSmartAuto"));
    }

    @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);
    }

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