package com.iqser.red.service.peristence.v1.server.integration.tests.performance;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.stream.Collectors;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.iqser.red.service.peristence.v1.server.integration.client.DossierClient;
import com.iqser.red.service.peristence.v1.server.integration.client.DossierStatsClient;
import com.iqser.red.service.peristence.v1.server.integration.client.FileClient;
import com.iqser.red.service.peristence.v1.server.integration.utils.AbstractPersistenceServerServiceTest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.Dossier;
import com.knecon.fforesight.tenantcommons.TenantContext;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FilePerformanceTest extends AbstractPersistenceServerServiceTest {

    private final static Integer DOSSIER_COUNT = 1;
    private final static Integer FILE_PER_DOSSIER_COUNT = 100;

    @Autowired
    private DossierClient dossierClient;

    @Autowired
    private FileClient fileClient;

    @Autowired
    private DossierStatsClient dossierStatsClient;

    @Autowired
    private PerformanceTestService performanceTestService;


    @BeforeEach
    public void setupTest() {

        TenantContext.setTenantId("redaction");
        performanceTestService.doSetup();
    }


    @Test
    public void doPerformanceTest() {

        long startAllDossiers = System.currentTimeMillis();
        var allDossiers = dossierClient.getDossiers(false, false);
        log.info("Load all dossiers time: {}ms", System.currentTimeMillis() - startAllDossiers);
        assertThat(allDossiers.size()).isEqualTo(DOSSIER_COUNT);

        long startDossierStats = System.currentTimeMillis();
        var dossierStats = dossierStatsClient.getDossierStats(allDossiers.stream().map(Dossier::getId).collect(Collectors.toSet()));
        log.info("Load all dossiers stats time: {}ms", System.currentTimeMillis() - startDossierStats);
        assertThat(dossierStats.size()).isEqualTo(DOSSIER_COUNT);

        for (var dossier : allDossiers) {
            long startFilesTime = System.currentTimeMillis();
            var files = fileClient.getDossierStatus(dossier.getId());
            assertThat(files.size()).isEqualTo(FILE_PER_DOSSIER_COUNT);
            log.info("Load files for dossier {} time: {}ms", dossier.getId(), System.currentTimeMillis() - startFilesTime);
        }

    }

}
