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

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

import java.util.List;
import java.util.Map;

import org.apache.http.HttpStatus;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;

import com.iqser.red.service.peristence.v1.server.integration.client.DictionaryClient;
import com.iqser.red.service.peristence.v1.server.integration.client.LegalBasisMappingClient;
import com.iqser.red.service.peristence.v1.server.integration.client.RulesClient;
import com.iqser.red.service.peristence.v1.server.integration.client.VersionClient;
import com.iqser.red.service.peristence.v1.server.integration.service.DossierTesterAndProvider;
import com.iqser.red.service.peristence.v1.server.integration.utils.AbstractPersistenceServerServiceTest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.CreateTypeValue;
import com.iqser.red.service.persistence.service.v1.api.shared.model.RuleFileType;
import com.iqser.red.service.persistence.service.v1.api.shared.model.VersionsResponse;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.legalbasis.LegalBasis;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.rules.DroolsValidationResponse;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.rules.RulesUploadRequestModel;

public class VersionsTest extends AbstractPersistenceServerServiceTest {

    @Autowired
    private DossierTesterAndProvider dossierTesterAndProvider;

    @Autowired
    private RulesClient rulesClient;
    @Autowired
    private VersionClient versionClient;
    @Autowired
    private DictionaryClient dictionaryClient;
    @Autowired
    private LegalBasisMappingClient legalBasisMapClient;


    @Test
    public void testVersionsResponse() {

        var dossier = dossierTesterAndProvider.provideTestDossier();

        Map<String, VersionsResponse> initialVersionsMap = versionClient.getVersions(List.of(dossier.getDossierTemplateId()));
        VersionsResponse initialVersions = initialVersionsMap.get(dossier.getDossierTemplateId());

        assertThat(initialVersions).isNotNull();

        long initialDictionaryVersion = initialVersions.getDictionaryVersion();
        long initialRulesVersion = initialVersions.getRulesVersion();
        long initialLegalBasisMapVersion = initialVersions.getLegalBasisMapVersion();

        var rulesUploadRequest = new RulesUploadRequestModel("lorem ipsum", dossier.getDossierTemplateId(), RuleFileType.ENTITY, false);
        ResponseEntity<DroolsValidationResponse> rulesResponse = rulesClient.upload(rulesUploadRequest);
        assertThat(rulesResponse.getStatusCode().value()).isEqualTo(HttpStatus.SC_OK);

        dictionaryClient.addType(CreateTypeValue.builder()
                                         .type("type_dossier")
                                         .label("Dossier Redactions")
                                         .hexColor("#fcba03")
                                         .rank(100)
                                         .description("Something")
                                         .addToDictionaryAction(false)
                                         .dossierTemplateId(dossier.getDossierTemplateId())
                                         .build());

        LegalBasis legalBasis = new LegalBasis();
        legalBasis.setName("Test Legal Basis");
        legalBasis.setDescription("This is a test legal basis.");
        legalBasis.setReason("Test Reason");
        legalBasis.setTechnicalName("test_legal_basis");

        legalBasisMapClient.addOrUpdateLegalBasis(dossier.getDossierTemplateId(), legalBasis);

        Map<String, VersionsResponse> updatedVersionsMap = versionClient.getVersions(List.of(dossier.getDossierTemplateId()));
        VersionsResponse updatedVersions = updatedVersionsMap.get(dossier.getDossierTemplateId());

        assertThat(updatedVersions.getRulesVersion()).isEqualTo(initialRulesVersion + 1);
        assertThat(updatedVersions.getDictionaryVersion()).isEqualTo(initialDictionaryVersion + 1);
        assertThat(updatedVersions.getLegalBasisMapVersion()).isEqualTo(initialLegalBasisMapVersion + 1);
    }

}

