package at.procon.dip.search.service;

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

import at.procon.dip.search.config.DipSearchProperties;
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 SearchLexicalIndexSchedulerTest {

    @Mock
    private DocumentLexicalIndexService lexicalIndexService;

    private DipSearchProperties properties;
    private SearchLexicalIndexScheduler scheduler;

    @BeforeEach
    void setUp() {
        properties = new DipSearchProperties();
        properties.setScheduledLexicalBackfillBatchSize(123);
        scheduler = new SearchLexicalIndexScheduler(properties, lexicalIndexService);
    }

    @Test
    void shouldBackfillMissingVectorsWhenEnabled() {
        properties.setScheduledLexicalBackfillEnabled(true);
        when(lexicalIndexService.backfillMissingVectors(123)).thenReturn(5);

        scheduler.backfillMissingLexicalIndexes();

        verify(lexicalIndexService).backfillMissingVectors(123);
    }

    @Test
    void shouldSkipBackfillWhenDisabled() {
        properties.setScheduledLexicalBackfillEnabled(false);

        scheduler.backfillMissingLexicalIndexes();

        verify(lexicalIndexService, never()).backfillMissingVectors(123);
    }
}
