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

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

import java.time.OffsetDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;

import com.google.common.collect.Sets;
import com.iqser.red.service.peristence.v1.server.integration.client.DictionaryClient;
import com.iqser.red.service.peristence.v1.server.integration.client.DossierClient;
import com.iqser.red.service.peristence.v1.server.integration.client.DossierStatusClient;
import com.iqser.red.service.peristence.v1.server.integration.client.ReportTemplateClient;
import com.iqser.red.service.peristence.v1.server.integration.client.WatermarkClient;
import com.iqser.red.service.peristence.v1.server.integration.service.DossierTemplateTesterAndProvider;
import com.iqser.red.service.peristence.v1.server.integration.service.DossierTesterAndProvider;
import com.iqser.red.service.peristence.v1.server.integration.service.TypeProvider;
import com.iqser.red.service.peristence.v1.server.integration.utils.AbstractPersistenceServerServiceTest;
import com.iqser.red.service.persistence.management.v1.processor.service.DossierManagementService;
import com.iqser.red.service.persistence.service.v1.api.shared.model.DossierRequest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.DossierStatusRequest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.WatermarkModel;
import com.iqser.red.service.persistence.service.v1.api.shared.model.common.JSONPrimitive;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.ReportTemplate;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.configuration.WatermarkOrientation;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.Dossier;

import feign.FeignException;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DossierTest extends AbstractPersistenceServerServiceTest {

    @Autowired
    private DossierTesterAndProvider dossierTesterAndProvider;

    @Autowired
    private DossierTemplateTesterAndProvider dossierTemplateTesterAndProvider;

    @Autowired
    private DossierClient dossierClient;

    @Autowired
    private ReportTemplateClient reportTemplateClient;

    @Autowired
    private DossierStatusClient dossierStatusClient;

    @Autowired
    private DictionaryClient dictionaryClient;

    @Autowired
    private WatermarkClient watermarkClient;

    @Autowired
    private DossierManagementService dossierManagementService;

    @Autowired
    private TypeProvider typeProvider;

    @Test
    @Disabled // TODO parallel does not work with tenantContext currently
    public void testDossierRaceCondition() {

        var dossierTemplate = dossierTemplateTesterAndProvider.provideTestTemplate();
        dossierTemplateTesterAndProvider.provideDefaultColors(dossierTemplate.getDossierTemplateId());
        IntStream.range(0, 10).parallel().forEach(x -> dossierTesterAndProvider.provideTestDossier(dossierTemplate, "race condition dossier: " + x));
        var allDossiers = dossierClient.getDossiers(true, true);
        var dossierCount = allDossiers.size();
        assertThat(dossierCount).isEqualTo(10);

    }


    @Test
    public void testCreateDuplicateDossier() {

        var dossierTemplate = dossierTemplateTesterAndProvider.provideTestTemplate();
        dossierTemplateTesterAndProvider.provideDefaultColors(dossierTemplate.getDossierTemplateId());
        IntStream.range(0, 8).parallel().forEach(x -> {
            try {
                dossierTesterAndProvider.provideTestDossier(dossierTemplate, "sameNameDossier");
            } catch (Exception e) {
                // conflict exception is expected
            }
        });
        var allDossiers = dossierClient.getDossiers(true, true);
        var dossierCount = allDossiers.size();
        assertThat(dossierCount).isEqualTo(1);
    }


    @Test
    public void testDossier() {

        var start = OffsetDateTime.now();

        var dossier = dossierTesterAndProvider.provideTestDossier();

        var returnedTypes = dictionaryClient.getAllTypes(dossier.getDossierTemplateId(), dossier.getDossierId(), false);
        assertThat(returnedTypes.getTypes().size()).isEqualTo(0);

        var watermark = new WatermarkModel();
        watermark.setName("watermark name");
        watermark.setEnabled(true);
        watermark.setText("Minions ipsum chasy para tu la bodaaa bananaaaa hana dul sae. Chasy hana dul sae pepete hana dul sae belloo! Tatata bala tu ti aamoo! Jeje.");
        watermark.setOrientation(WatermarkOrientation.DIAGONAL);
        watermark.setDossierTemplateId(dossier.getDossierTemplateId());

        var watermarkConfig = watermarkClient.saveWatermark(watermark);

        watermark.setEnabled(false);
        watermark.setName("watermark disabled");
        var watermarkConfigDisabled = watermarkClient.saveWatermark(watermark);

        var allDossiers = dossierClient.getDossiers(false, false);
        assertThat(allDossiers.size()).isEqualTo(1);
        assertThat(allDossiers.get(0)).isEqualTo(dossier);

        var changes = dossierClient.changesSince(JSONPrimitive.of(start));
        assertThat(changes).isNotEmpty();

        // update with a disabled preview watermark
        var cru = new DossierRequest();
        cru.setDossierTemplateId(dossier.getId());
        cru.setWatermarkId(watermarkConfig.getId());
        BeanUtils.copyProperties(dossier, cru);
        cru.setDossierName("Dossier 1 Update");
        cru.setPreviewWatermarkId(watermarkConfigDisabled.getId());
        // Create dossier status
        var crudsr = new DossierStatusRequest();
        crudsr.setName("name1");
        crudsr.setColor("#cccccc");
        crudsr.setDescription("description1");
        crudsr.setDossierTemplateId(dossier.getDossierTemplateId());

        var loadedDossierStatus = dossierStatusClient.createOrUpdateDossierStatus(crudsr);
        assertThat(loadedDossierStatus).isNotNull();

        cru.setDossierStatusId(loadedDossierStatus.getId());

        try {
            dossierClient.createDossierOrUpdateDossier(cru);
        } catch (FeignException e) {
            assertThat(e.status()).isEqualTo(400);
        }

        cru.setPreviewWatermarkId(watermarkConfig.getId());
        // update the watermark preview to an enabled one
        cru.setDossierId(dossier.getId());
        var updated = dossierClient.createDossierOrUpdateDossier(cru).getBody();
        assertThat(updated.getDossierName()).isEqualTo("Dossier 1 Update");
        assertThat(updated.getWatermarkId()).isNull();
        assertThat(updated.getPreviewWatermarkId()).isEqualTo(watermarkConfig.getId());
        assertThat(updated.getDossierStatusId()).isEqualTo(loadedDossierStatus.getId());

        assertThat(watermarkClient.isWatermarkUsed(watermarkConfig.getId())).isEqualTo(JSONPrimitive.of(true));

        // disable the watermark used
        watermarkConfig.setEnabled(false);
        var watermarkDisabled = watermarkClient.saveWatermark(watermarkConfig);
        assertThat(watermarkDisabled.isEnabled()).isFalse();
        assertThat(watermarkDisabled.getId()).isEqualTo(watermarkConfig.getId());
        // update dossier description, while the watermark used was disabled
        cru.setDescription("new description");
        updated = dossierClient.createDossierOrUpdateDossier(cru).getBody();
        assertThat(updated.getDescription()).isEqualTo(cru.getDescription());
        assertThat(updated.getPreviewWatermarkId()).isEqualTo(watermarkConfig.getId());
        assertThat(updated.getWatermarkId()).isNull();

        cru.setPreviewWatermarkId(watermarkConfigDisabled.getId());
        //try to update the dossier with a different disabled watermark
        try {
            dossierClient.createDossierOrUpdateDossier(cru);
        } catch (FeignException e) {
            assertThat(e.status()).isEqualTo(400);
        }

        // put dossier status to null
        cru.setPreviewWatermarkId(watermarkConfig.getId());
        cru.setDossierStatusId(null);
        updated = dossierClient.createDossierOrUpdateDossier(cru).getBody();
        assertThat(updated.getDossierStatusId()).isNull();

        var loadedTemplate = dossierClient.getDossier(updated.getId(), false, false);

        assertThat(loadedTemplate).isEqualTo(updated);

        dossierClient.getDossiers(false, false).forEach(ld -> dossierClient.deleteDossier(ld.getId()));

        assertThat(dossierClient.getDossiers(false, false)).isEmpty();
        assertThat(dossierClient.getSoftDeletedDossiers().size()).isEqualTo(1);
        assertThat(dossierClient.getDossiers(false, true)).hasSize(1);

        dossierClient.undeleteDossiers(Sets.newHashSet(dossier.getId()));

        assertThat(dossierClient.getDossiers(false, false)).isNotEmpty();
        assertThat(dossierClient.getSoftDeletedDossiers()).isEmpty();

        dossierClient.hardDeleteDossiers(Sets.newHashSet(dossier.getId()));
        assertThat(dossierClient.getDossiers(false, false)).isEmpty();
        assertThat(dossierClient.getDossiers(true, false)).isEmpty();
        assertThat(dossierClient.getDossiers(false, true)).hasSize(0);
        assertThat(dossierClient.getDossiers(true, true)).hasSize(0);
        assertThat(dossierClient.getSoftDeletedDossiers()).isEmpty();

        assertThatThrownBy(() -> dossierClient.undeleteDossiers(Sets.newHashSet(dossier.getId()))).isInstanceOf(FeignException.class);

        var dossierInformation = dossierClient.getDossierInformation();
        assertThat(dossierInformation.getNumberOfActiveDossiers()).isEqualTo(0);
        assertThat(dossierInformation.getNumberOfSoftDeletedDossiers()).isEqualTo(0);
//        assertThat(dossierInformation.getNumberOfHardDeletedDossiers()).isEqualTo(0);
        assertThat(dossierInformation.getNumberOfArchivedDossiers()).isEqualTo(0);
    }


    @Test
    public void testArchiveDossier() {

        var template = dossierTemplateTesterAndProvider.provideTestTemplate();

        // Arrange
        var dossier1 = dossierTesterAndProvider.provideTestDossier(template, "dossier1");
        var dossier2 = dossierTesterAndProvider.provideTestDossier(template, "dossier2");

        // Act & Assert 1
        assertThat(dossierClient.getDossiers(false, false)).hasSize(2);
        assertThat(dossierClient.getDossiers(true, false)).hasSize(2);
        assertThat(dossierClient.getDossiers(false, true)).hasSize(2);
        assertThat(dossierClient.getDossiers(true, true)).hasSize(2);
        assertThat(dossierClient.getArchivedDossiers()).hasSize(0);
        assertThat(dossierClient.getArchivedDossiersForDossierTemplate(dossier1.getDossierTemplateId())).hasSize(0);
        assertThat(dossierClient.getDossiersForDossierTemplate(dossier1.getDossierTemplateId(), true, true)).hasSize(2);
        assertThat(dossierClient.getSoftDeletedDossiers()).hasSize(0);

        // Act & Assert 2
        dossierClient.archiveDossiers(Set.of(dossier1.getId()));
        assertThat(dossierClient.getDossiers(false, false)).hasSize(1);
        assertThat(dossierClient.getDossiers(true, false)).hasSize(2);
        assertThat(dossierClient.getDossiers(false, true)).hasSize(1);
        assertThat(dossierClient.getDossiers(true, true)).hasSize(2);
        assertThat(dossierClient.getArchivedDossiers()).hasSize(1);
        assertThat(dossierClient.getSoftDeletedDossiers()).hasSize(0);
        assertThat(dossierClient.getArchivedDossiersForDossierTemplate(dossier1.getDossierTemplateId())).hasSize(1);
        assertThat(dossierClient.getDossiersForDossierTemplate(dossier1.getDossierTemplateId(), true, true)).hasSize(2);

        var dossierInformation = dossierClient.getDossierInformation();
//        assertThat(dossierInformation.getNumberOfActiveDossiers()).isEqualTo(1);
        assertThat(dossierInformation.getNumberOfSoftDeletedDossiers()).isEqualTo(0);
        assertThat(dossierInformation.getNumberOfHardDeletedDossiers()).isEqualTo(0);
//        assertThat(dossierInformation.getNumberOfArchivedDossiers()).isEqualTo(1);

        // Act & Assert 3
        dossierManagementService.unarchiveDossiers(Set.of(dossier1.getId()));
        assertThat(dossierClient.getDossiers(false, false)).hasSize(2);
        assertThat(dossierClient.getArchivedDossiers()).hasSize(0);

        dossierInformation = dossierClient.getDossierInformation();
//        assertThat(dossierInformation.getNumberOfActiveDossiers()).isEqualTo(2);
        assertThat(dossierInformation.getNumberOfSoftDeletedDossiers()).isEqualTo(0);
        assertThat(dossierInformation.getNumberOfHardDeletedDossiers()).isEqualTo(0);
        assertThat(dossierInformation.getNumberOfArchivedDossiers()).isEqualTo(0);

        // Act & Assert 4
        dossierClient.archiveDossiers(Set.of(dossier1.getId(), dossier2.getId()));
        assertThat(dossierClient.getDossiers(false, false)).hasSize(0);
        assertThat(dossierClient.getDossiers(true, false)).hasSize(2);
        assertThat(dossierClient.getArchivedDossiers()).hasSize(2);
        assertThat(dossierClient.getSoftDeletedDossiers()).hasSize(0);

        dossierInformation = dossierClient.getDossierInformation();
        assertThat(dossierInformation.getNumberOfActiveDossiers()).isEqualTo(0);
        assertThat(dossierInformation.getNumberOfSoftDeletedDossiers()).isEqualTo(0);
        assertThat(dossierInformation.getNumberOfHardDeletedDossiers()).isEqualTo(0);
//        assertThat(dossierInformation.getNumberOfArchivedDossiers()).isEqualTo(2);

        // Act & Assert 5
        dossierClient.deleteDossier(dossier1.getId());
        dossierClient.archiveDossiers(Set.of(dossier1.getId(), dossier2.getId()));
        assertThat(dossierClient.getDossiers(false, false)).hasSize(0);
        assertThat(dossierClient.getDossiers(true, false)).hasSize(1);
        assertThat(dossierClient.getDossiers(false, true)).hasSize(1);
        assertThat(dossierClient.getDossiers(true, true)).hasSize(2);
        assertThat(dossierClient.getArchivedDossiers()).hasSize(1);
        assertThat(dossierClient.getSoftDeletedDossiers()).hasSize(1);

        dossierInformation = dossierClient.getDossierInformation();
        assertThat(dossierInformation.getNumberOfActiveDossiers()).isEqualTo(0);
//        assertThat(dossierInformation.getNumberOfSoftDeletedDossiers()).isEqualTo(1);
        assertThat(dossierInformation.getNumberOfHardDeletedDossiers()).isEqualTo(0);
//        assertThat(dossierInformation.getNumberOfArchivedDossiers()).isEqualTo(1);

        // Act & Assert 6
        dossierManagementService.unarchiveDossiers(Set.of(dossier1.getId(), dossier2.getId()));
        assertThat(dossierClient.getDossiers(false, false)).hasSize(1);
        assertThat(dossierClient.getDossiers(true, false)).hasSize(1);
        assertThat(dossierClient.getDossiers(false, true)).hasSize(2);
        assertThat(dossierClient.getArchivedDossiers()).hasSize(0);
        assertThat(dossierClient.getSoftDeletedDossiers()).hasSize(1);

        dossierInformation = dossierClient.getDossierInformation();
//        assertThat(dossierInformation.getNumberOfActiveDossiers()).isEqualTo(1);
//        assertThat(dossierInformation.getNumberOfSoftDeletedDossiers()).isEqualTo(1);
        assertThat(dossierInformation.getNumberOfHardDeletedDossiers()).isEqualTo(0);
        assertThat(dossierInformation.getNumberOfArchivedDossiers()).isEqualTo(0);

        // Act & Assert 7
        dossierClient.hardDeleteDossiers(Set.of(dossier1.getId()));
        dossierClient.archiveDossiers(Set.of(dossier1.getId(), dossier2.getId()));
        assertThat(dossierClient.getDossiers(false, false)).hasSize(0);
        assertThat(dossierClient.getDossiers(true, false)).hasSize(1);
        assertThat(dossierClient.getDossiers(false, true)).hasSize(0);
        assertThat(dossierClient.getDossiers(true, true)).hasSize(1);
        assertThat(dossierClient.getArchivedDossiers()).hasSize(1);
        assertThat(dossierClient.getSoftDeletedDossiers()).hasSize(0);

        dossierInformation = dossierClient.getDossierInformation();
        assertThat(dossierInformation.getNumberOfActiveDossiers()).isEqualTo(0);
        assertThat(dossierInformation.getNumberOfSoftDeletedDossiers()).isEqualTo(0);
//        assertThat(dossierInformation.getNumberOfHardDeletedDossiers()).isEqualTo(0);
//        assertThat(dossierInformation.getNumberOfArchivedDossiers()).isEqualTo(1);

    }


    @Test
    public void testDossierWithReportTemplates() {

        var dossier = dossierTesterAndProvider.provideTestDossier();
        var watermark = new WatermarkModel();
        watermark.setName("watermark name");
        watermark.setEnabled(true);
        watermark.setText("Minions ipsum chasy para tu la bodaaa bananaaaa hana dul sae. Chasy hana dul sae pepete hana dul sae belloo! Tatata bala tu ti aamoo! Jeje.");
        watermark.setOrientation(WatermarkOrientation.DIAGONAL);
        watermark.setDossierTemplateId(dossier.getDossierTemplateId());

        var watermarkConfig = watermarkClient.saveWatermark(watermark);

        String dossierName = "Dossier 1 Update";

        var allDossiers = dossierClient.getDossiers(false, false);
        assertThat(allDossiers.size()).isEqualTo(1);
        assertThat(allDossiers.get(0)).isEqualTo(dossier);

        List<ReportTemplate> availableTemplates = provideTestReportTemplates(dossier);

        // 1. update dossier: Add 3 report templates, but only on exists
        var cru = new DossierRequest();
        cru.setDossierTemplateId(dossier.getId());
        BeanUtils.copyProperties(dossier, cru);
        cru.setDossierName(dossierName);
        cru.setPreviewWatermarkId(watermarkConfig.getId());
        cru.setReportTemplateIds(Set.of(availableTemplates.get(0).getTemplateId(), "id-does-not-exist-1", "id-does-not-exist-2"));

        var updated = dossierClient.createDossierOrUpdateDossier(cru).getBody();
        assertThat(updated.getDossierName()).isEqualTo(dossierName);
        assertThat(updated.getPreviewWatermarkId()).isEqualTo(watermarkConfig.getId());
        assertThat(updated.getReportTemplateIds().size()).isEqualTo(1);
        assertThat(updated.getReportTemplateIds()).contains(availableTemplates.get(0).getTemplateId());

        var loadedTemplate = dossierClient.getDossier(dossier.getId(), false, false);

        assertThat(loadedTemplate).isEqualTo(updated);
        assertThat(loadedTemplate.getReportTemplateIds().size()).isEqualTo(1);
        assertThat(loadedTemplate.getReportTemplateIds()).contains(availableTemplates.get(0).getTemplateId());

        allDossiers = dossierClient.getDossiers(false, false);
        assertThat(allDossiers.size()).isEqualTo(1);
        assertThat(allDossiers.get(0).getReportTemplateIds().size()).isEqualTo(1);

        // 2. update dossier: Add 2 report templates, but one is already added
        cru = new DossierRequest();
        cru.setDossierTemplateId(dossier.getId());
        BeanUtils.copyProperties(dossier, cru);
        cru.setDossierName(dossierName);
        cru.setPreviewWatermarkId(watermarkConfig.getId());
        cru.setReportTemplateIds(Set.of(availableTemplates.get(0).getTemplateId(), availableTemplates.get(1).getTemplateId()));

        updated = dossierClient.createDossierOrUpdateDossier(cru).getBody();
        assertThat(updated.getDossierName()).isEqualTo(dossierName);
        assertThat(updated.getPreviewWatermarkId()).isEqualTo(watermarkConfig.getId());
        assertThat(updated.getReportTemplateIds().size()).isEqualTo(2);
        assertThat(updated.getReportTemplateIds()).contains(availableTemplates.get(0).getTemplateId(), availableTemplates.get(1).getTemplateId());

        loadedTemplate = dossierClient.getDossier(dossier.getId(), false, false);

        assertThat(loadedTemplate).isEqualTo(updated);
        assertThat(loadedTemplate.getReportTemplateIds().size()).isEqualTo(2);
        assertThat(loadedTemplate.getReportTemplateIds()).contains(availableTemplates.get(0).getTemplateId(), availableTemplates.get(1).getTemplateId());

        // 3. update dossier: Add 2 report templates, but one is already added. One has to be removed
        cru = new DossierRequest();
        cru.setDossierTemplateId(dossier.getId());
        BeanUtils.copyProperties(dossier, cru);
        cru.setDossierName(dossierName);
        cru.setPreviewWatermarkId(watermarkConfig.getId());
        cru.setReportTemplateIds(Set.of(availableTemplates.get(1).getTemplateId(), availableTemplates.get(2).getTemplateId()));

        updated = dossierClient.createDossierOrUpdateDossier(cru).getBody();
        assertThat(updated.getDossierName()).isEqualTo(dossierName);
        assertThat(updated.getPreviewWatermarkId()).isEqualTo(watermarkConfig.getId());
        assertThat(updated.getReportTemplateIds().size()).isEqualTo(2);
        assertThat(updated.getReportTemplateIds()).contains(availableTemplates.get(1).getTemplateId(), availableTemplates.get(2).getTemplateId());

        loadedTemplate = dossierClient.getDossier(dossier.getId(), false, false);

        assertThat(loadedTemplate).isEqualTo(updated);
        assertThat(loadedTemplate.getReportTemplateIds().size()).isEqualTo(2);
        assertThat(loadedTemplate.getReportTemplateIds()).contains(availableTemplates.get(1).getTemplateId(), availableTemplates.get(2).getTemplateId());

        // 4. update dossier: Add 1 report templates. The others have to be removed
        cru = new DossierRequest();
        cru.setDossierTemplateId(dossier.getId());
        BeanUtils.copyProperties(dossier, cru);
        cru.setDossierName(dossierName);
        cru.setPreviewWatermarkId(watermarkConfig.getId());
        cru.setReportTemplateIds(Set.of(availableTemplates.get(0).getTemplateId()));

        updated = dossierClient.createDossierOrUpdateDossier(cru).getBody();
        assertThat(updated.getDossierName()).isEqualTo(dossierName);
        assertThat(updated.getPreviewWatermarkId()).isEqualTo(watermarkConfig.getId());
        assertThat(updated.getReportTemplateIds().size()).isEqualTo(1);
        assertThat(updated.getReportTemplateIds()).contains(availableTemplates.get(0).getTemplateId());

        loadedTemplate = dossierClient.getDossier(dossier.getId(), false, false);

        assertThat(loadedTemplate).isEqualTo(updated);
        assertThat(loadedTemplate.getReportTemplateIds().size()).isEqualTo(1);
        assertThat(loadedTemplate.getReportTemplateIds()).contains(availableTemplates.get(0).getTemplateId());

        // Remove dossier
        dossierClient.getDossiers(false, false).forEach(ld -> dossierClient.deleteDossier(ld.getId()));
        dossierClient.hardDeleteDossiers(Sets.newHashSet(dossier.getId()));
        assertThat(dossierClient.getDossiers(false, false)).isEmpty();
        assertThat(dossierClient.getSoftDeletedDossiers()).isEmpty();
    }


    private List<ReportTemplate> provideTestReportTemplates(Dossier dossier) {

        var template1 = new MockMultipartFile("reportTemplate1.docx","reportTemplate1.docx","application/word" ,new byte[]{1, 2, 3, 4});
        var template2 = new MockMultipartFile("reportTemplate2.docx","reportTemplate2.docx","application/word" ,new byte[]{1, 2, 3, 4});
        var template3 = new MockMultipartFile("reportTemplate2.docx","reportTemplate3.docx","application/word" ,new byte[]{1, 2, 3, 4});

        reportTemplateClient.uploadTemplate(template1, dossier.getDossierTemplateId(), true, false);
        reportTemplateClient.uploadTemplate(template2, dossier.getDossierTemplateId(), true, false);
        reportTemplateClient.uploadTemplate(template3, dossier.getDossierTemplateId(), true, false);

        var availableTemplates = reportTemplateClient.getAvailableReportTemplates(dossier.getDossierTemplateId());
        assertThat(availableTemplates.size()).isEqualTo(3);
        return availableTemplates;
    }


    @Test
    public void testgetDossiersWithoutDeleted() {

        var template = dossierTemplateTesterAndProvider.provideTestTemplate();

        // Arrange
        var dossier1 = dossierTesterAndProvider.provideTestDossier(template, "dossier1");

        var nonDeletedDossiers = dossierClient.getDossiers(true, false);
        assertThat(nonDeletedDossiers).hasSize(1);

        dossierClient.archiveDossiers(Set.of(dossier1.getId()));
        nonDeletedDossiers = dossierClient.getDossiers(true, false);
        assertThat(nonDeletedDossiers).hasSize(1);

        dossierClient.deleteDossier(dossier1.getId());
        nonDeletedDossiers = dossierClient.getDossiers(true, false);
        assertThat(nonDeletedDossiers).hasSize(0);
        assertThat(dossierClient.getSoftDeletedDossiers()).hasSize(1);

        dossierClient.hardDeleteDossiers(Set.of(dossier1.getId()));
        nonDeletedDossiers = dossierClient.getDossiers(true, false);
        assertThat(nonDeletedDossiers).hasSize(0);
        assertThat(dossierClient.getSoftDeletedDossiers()).hasSize(0);

    }


    @Test
    public void testCheckIncludeArchivedAndIncludeDeleted() {

        var template = dossierTemplateTesterAndProvider.provideTestTemplate();

        // Arrange
        var regularDossier = dossierTesterAndProvider.provideTestDossier(template, "regularDossier");
        var onlyArchivedDossier = dossierTesterAndProvider.provideTestDossier(template, "onlyArchivedDossier");
        var onlyDeletedDossier = dossierTesterAndProvider.provideTestDossier(template, "onlyDeletedDossier");
        var archivedAndDeletedDossier = dossierTesterAndProvider.provideTestDossier(template, "archivedAndDeletedDossier");
        var hardDeletedDossier = dossierTesterAndProvider.provideTestDossier(template, "hardDeletedDossier");

        dossierClient.archiveDossiers(Set.of(onlyArchivedDossier.getId(), archivedAndDeletedDossier.getId()));
        dossierClient.deleteDossier(onlyDeletedDossier.getId());
        dossierClient.deleteDossier(archivedAndDeletedDossier.getId());
        dossierClient.deleteDossier(hardDeletedDossier.getId());
        dossierClient.hardDeleteDossiers(Set.of(hardDeletedDossier.getId()));

        // check get all dossiers with inclusions
        assertThat(dossierClient.getDossiers(true, true)).hasSize(4);        //all without harddeleted
        assertThat(dossierClient.getDossiers(true, false)).hasSize(2);       //regular and onlyarchived
        assertThat(dossierClient.getDossiers(false, true)).hasSize(3);       //regular and onlydeleted and archivedanddeleted
        assertThat(dossierClient.getDossiers(false, false)).hasSize(1);      //regular

    }

}
