package at.procon.dip.vectorization.startup;

import at.procon.dip.domain.document.service.DocumentEmbeddingService;
import at.procon.dip.domain.document.service.command.RegisterEmbeddingModelCommand;
import at.procon.dip.runtime.condition.ConditionalOnRuntimeMode;
import at.procon.dip.runtime.config.RuntimeMode;
import at.procon.ted.config.LegacyVectorizationProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * Ensures the configured embedding model exists in DOC.doc_embedding_model for the legacy runtime path.
 */
@Component
@ConditionalOnRuntimeMode(RuntimeMode.LEGACY)
@RequiredArgsConstructor
@Slf4j
public class ConfiguredEmbeddingModelStartupRunner implements ApplicationRunner {

    private final LegacyVectorizationProperties properties;
    private final DocumentEmbeddingService embeddingService;

    @Override
    public void run(ApplicationArguments args) {
        if (!properties.isEnabled() || !properties.isGenericPipelineEnabled()) {
            return;
        }

        embeddingService.registerModel(new RegisterEmbeddingModelCommand(
                properties.getModelName(),
                properties.getEmbeddingProvider(),
                properties.getModelName(),
                properties.getDimensions(),
                null,
                false,
                true
        ));

        log.info("Legacy embedding model ensured: {}", properties.getModelName());
    }
}