package at.procon.dip.embedding.registry;

import at.procon.dip.embedding.config.EmbeddingProperties;
import at.procon.dip.embedding.model.ResolvedEmbeddingProviderConfig;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class EmbeddingProviderConfigResolver {

    private final EmbeddingProperties properties;

    public ResolvedEmbeddingProviderConfig resolve(String providerConfigKey) {
        EmbeddingProperties.ProviderProperties provider = properties.getProviders().get(providerConfigKey);
        if (provider == null) {
            throw new IllegalArgumentException("Unknown embedding provider config key: " + providerConfigKey);
        }

        EmbeddingProperties.BatchRequestProperties batchRequest = provider.getBatchRequest() == null
                ? new EmbeddingProperties.BatchRequestProperties()
                : provider.getBatchRequest();

        return ResolvedEmbeddingProviderConfig.builder()
                .key(providerConfigKey)
                .providerType(provider.getType())
                .baseUrl(provider.getBaseUrl())
                .apiKey(provider.getApiKey())
                .connectTimeout(provider.getConnectTimeout())
                .readTimeout(provider.getReadTimeout())
                .headers(provider.getHeaders() == null ? Map.of() : Map.copyOf(provider.getHeaders()))
                .dimensions(provider.getDimensions())
                .batchTruncateText(batchRequest.isTruncateText())
                .batchTruncateLength(batchRequest.getTruncateLength())
                .batchChunkSize(batchRequest.getChunkSize())
                .build();
    }
}
