package at.procon.dip.embedding.startup;

import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import at.procon.dip.domain.document.DocumentType;
import at.procon.dip.domain.document.RepresentationType;
import at.procon.dip.embedding.config.EmbeddingProperties;
import at.procon.dip.embedding.service.RepresentationEmbeddingOrchestrator;
import at.procon.dip.embedding.service.ScopedEmbeddingEnqueueResult;
import at.procon.dip.embedding.service.ScopedEmbeddingEnqueueService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class EmbeddingStartupRunnerTest {

    @Mock
    private ScopedEmbeddingEnqueueService enqueueService;
    @Mock
    private RepresentationEmbeddingOrchestrator orchestrator;

    private EmbeddingProperties properties;
    private EmbeddingStartupRunner runner;

    @BeforeEach
    void setUp() {
        properties = new EmbeddingProperties();
        properties.setEnabled(true);
        runner = new EmbeddingStartupRunner(properties, enqueueService, orchestrator);
    }

    @Test
    void startupCanEnqueueAllMissingTedLotEmbeddingsAcrossBatches() {
        EmbeddingProperties.StartupProperties startup = properties.getStartup();
        startup.setEnqueueMissingEnabled(true);
        startup.setDocumentType(DocumentType.TED_NOTICE_LOT);
        startup.setRepresentationType(RepresentationType.SEMANTIC_TEXT);
        startup.setPrimaryOnly(true);
        startup.setBatchSize(2);

        when(enqueueService.enqueueByDocumentType(
                DocumentType.TED_NOTICE_LOT,
                RepresentationType.SEMANTIC_TEXT,
                null,
                true,
                null,
                false,
                0,
                2
        )).thenReturn(new ScopedEmbeddingEnqueueResult(
                DocumentType.TED_NOTICE_LOT, RepresentationType.SEMANTIC_TEXT, null, true, "e5-default", false, 2, 2, 2));
        when(enqueueService.enqueueByDocumentType(
                DocumentType.TED_NOTICE_LOT,
                RepresentationType.SEMANTIC_TEXT,
                null,
                true,
                null,
                false,
                1,
                2
        )).thenReturn(new ScopedEmbeddingEnqueueResult(
                DocumentType.TED_NOTICE_LOT, RepresentationType.SEMANTIC_TEXT, null, true, "e5-default", false, 2, 1, 1));

        runner.run(null);

        verify(enqueueService).enqueueByDocumentType(
                DocumentType.TED_NOTICE_LOT, RepresentationType.SEMANTIC_TEXT, null, true, null, false, 0, 2);
        verify(enqueueService).enqueueByDocumentType(
                DocumentType.TED_NOTICE_LOT, RepresentationType.SEMANTIC_TEXT, null, true, null, false, 1, 2);
        verify(orchestrator, never()).processNextReadyBatch();
    }

    @Test
    void startupCanProcessReadyJobsWhenJobSubsystemIsEnabled() {
        properties.getStartup().setProcessReadyEnabled(true);
        properties.getJobs().setEnabled(true);
        when(orchestrator.processNextReadyBatch()).thenReturn(3);

        runner.run(null);

        verify(orchestrator).processNextReadyBatch();
    }
}
