package at.procon.dip.embedding.service;

import at.procon.dip.domain.document.entity.DocumentEmbeddingModel;
import at.procon.dip.domain.document.service.DocumentEmbeddingService;
import at.procon.dip.domain.document.service.command.RegisterEmbeddingModelCommand;
import at.procon.dip.embedding.model.EmbeddingModelDescriptor;
import at.procon.dip.embedding.registry.EmbeddingModelRegistry;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional
public class EmbeddingModelCatalogService {

    private final EmbeddingModelRegistry modelRegistry;
    private final DocumentEmbeddingService documentEmbeddingService;

    public DocumentEmbeddingModel ensureRegistered(String modelKey) {
        EmbeddingModelDescriptor descriptor = modelRegistry.getRequired(modelKey);
        documentEmbeddingService.registerModel(new RegisterEmbeddingModelCommand(
                descriptor.modelKey(),
                descriptor.providerConfigKey(),
                descriptor.providerModelKey(),
                descriptor.dimensions(),
                descriptor.distanceMetric(),
                descriptor.supportsQueryEmbeddingMode(),
                descriptor.active()
        ));
        return documentEmbeddingService.findActiveModelByKey(modelKey);
    }
}
