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

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

import java.util.ArrayList;
import java.util.stream.Collectors;

import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.iqser.red.service.peristence.v1.server.integration.client.LegalBasisClient;
import com.iqser.red.service.peristence.v1.server.integration.client.VersionClient;
import com.iqser.red.service.peristence.v1.server.integration.service.DossierTemplateTesterAndProvider;
import com.iqser.red.service.peristence.v1.server.integration.utils.AbstractPersistenceServerServiceTest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.legalbasis.LegalBasis;

import feign.FeignException;

public class LegalBasisTest extends AbstractPersistenceServerServiceTest {

    @Autowired
    private DossierTemplateTesterAndProvider dossierTemplateTesterAndProvider;

    @Autowired
    private LegalBasisClient legalBasisClient;

    @Autowired
    private VersionClient versionClient;


    @Test
    public void testLegalBasis() {

        var dossierTemplate = dossierTemplateTesterAndProvider.provideTestTemplate();

        try {
            legalBasisClient.getLegalBasisMapping(dossierTemplate.getId());
        } catch (FeignException.FeignClientException e) {
            assertThat(e.status()).isEqualTo(404);
        }

        var mappings = new ArrayList<LegalBasis>();
        mappings.add(LegalBasis.builder().name("test 1").description("test 1").reason("test 1").build());
        mappings.add(LegalBasis.builder().name("test 2").description("test 2").reason("test 2").build());
        legalBasisClient.setLegalBasisMapping(mappings, dossierTemplate.getId());

        var mapping = legalBasisClient.getLegalBasisMapping(dossierTemplate.getId());
        assertThat(mapping.size()).isEqualTo(2);
        assertThat(mapping.stream().map(LegalBasis::getName).collect(Collectors.toList())).containsExactlyInAnyOrder("test 1", "test 2");

        var legalBasis = LegalBasis.builder().name("test 3").description("test 3").reason("test 3").build();
        legalBasisClient.addOrUpdateLegalBasis(dossierTemplate.getId(), legalBasis);

        mapping = legalBasisClient.getLegalBasisMapping(dossierTemplate.getId());
        assertThat(mapping.size()).isEqualTo(3);
        assertThat(mapping.stream().map(LegalBasis::getName).collect(Collectors.toList())).containsExactlyInAnyOrder("test 1", "test 2", "test 3");

        legalBasis = LegalBasis.builder().name("test 3").reason("test 3").description("test 3 - updated").build();
        legalBasisClient.addOrUpdateLegalBasis(dossierTemplate.getId(), legalBasis);

        mapping = legalBasisClient.getLegalBasisMapping(dossierTemplate.getId());
        assertThat(mapping.size()).isEqualTo(3);
        assertThat(mapping.stream().map(LegalBasis::getDescription).collect(Collectors.toList())).containsExactlyInAnyOrder("test 1", "test 2", "test 3 - updated");

        legalBasisClient.deleteLegalBasis(dossierTemplate.getId(), Lists.newArrayList("test 1"));
        mapping = legalBasisClient.getLegalBasisMapping(dossierTemplate.getId());
        assertThat(mapping.size()).isEqualTo(2);

        legalBasisClient.setLegalBasisMapping(Lists.newArrayList(), dossierTemplate.getId());
        assertThat(legalBasisClient.getLegalBasisMapping(dossierTemplate.getId())).isEmpty();

    }

}
