package at.procon.dip.embedding.model;

import java.util.List;

public final class EmbeddingInputPrefixing {

    private EmbeddingInputPrefixing() {
    }

    public static PrefixedTexts apply(EmbeddingModelDescriptor model, EmbeddingUseCase useCase, List<String> texts) {
        EmbeddingPrefixMode prefixMode = model.prefixMode() == null ? EmbeddingPrefixMode.OFF : model.prefixMode();
        return switch (prefixMode) {
            case OFF -> new PrefixedTexts(texts, EmbeddingPrefixMode.OFF, null);
            case EXTERNAL -> new PrefixedTexts(texts, EmbeddingPrefixMode.EXTERNAL, null);
            case CLIENT -> {
                String prefix = useCase == EmbeddingUseCase.QUERY ? model.queryPrefix() : model.documentPrefix();
                if (prefix == null || prefix.isBlank()) {
                    throw new IllegalStateException("Prefix mode CLIENT requires a non-blank prefix for use case " + useCase);
                }
                yield new PrefixedTexts(
                        texts.stream().map(text -> prefix + text).toList(),
                        EmbeddingPrefixMode.CLIENT,
                        prefix
                );
            }
        };
    }

    public record PrefixedTexts(
            List<String> texts,
            EmbeddingPrefixMode prefixMode,
            String appliedPrefix
    ) {
    }
}
