package at.procon.dip.clustering.config;

import at.procon.dip.clustering.PythonRequestMode;
import java.time.Duration;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "dip.clustering.python")
public record ClusteringPhaseBProperties(
        boolean enabled,
        String baseUrl,
        String clusterPath,
        String clusterRunPath,
        Duration connectTimeout,
        Duration readTimeout,
        PythonRequestMode requestMode
) {
    public String resolvedClusterUrl() {
        return resolveUrl(clusterPath == null || clusterPath.isBlank() ? "/cluster" : clusterPath);
    }

    public String resolvedClusterRunUrl() {
        return resolveUrl(clusterRunPath == null || clusterRunPath.isBlank() ? "/cluster-run" : clusterRunPath);
    }

    public PythonRequestMode effectiveRequestMode() {
        return requestMode == null ? PythonRequestMode.INLINE_VECTORS : requestMode;
    }

    private String resolveUrl(String path) {
        if (baseUrl == null || baseUrl.isBlank()) {
            return null;
        }
        return baseUrl.endsWith("/")
                ? baseUrl.substring(0, baseUrl.length() - 1) + path
                : baseUrl + path;
    }
}
