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

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 org.apache.commons.lang3.tuple.Pair;

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

/**
 * SZR = Správa základních registrů - https://www.szrcr.cz/
 * Test environments verify identity against the mock server - https://confluence.moneta.cz/display/LAB8/Dokumentace+Mock+APIs
 */

public class SzrMockApiEndpoint implements RestEndpoint {
    private final RestConnector restConnector;
    private final StoreAccessor store;

    public SzrMockApiEndpoint(StoreAccessor store) {
        this.store = store;
        String endpointName = "endpoints.szr-mock-api.url" ;
        this.restConnector = Optional.ofNullable(store.getConfig(endpointName))
                .map(url->new SimpleRestConnector(url,"SzrMockApiLogger"))
                .orElseThrow(()-> new IllegalStateException("You need to configure " + endpointName + " to work with SzrMockApi"));
    }

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