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

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

import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Sets;
import com.iqser.red.service.peristence.v1.server.integration.client.DossierClient;
import com.iqser.red.service.persistence.service.v1.api.shared.model.DossierRequest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.DossierTemplateModel;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.DownloadFileType;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.Dossier;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.DossierStatusInfo;
import com.knecon.fforesight.tenantcommons.TenantContext;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class DossierTesterAndProvider {

    @Autowired
    private DossierTemplateTesterAndProvider dossierTemplateTesterAndProvider;

    @Autowired
    private DossierClient dossierClient;

    @Autowired
    private UserProvider userProvider;


    public Dossier provideTestDossier(DossierTemplateModel testTemplate) {

        return provideTestDossier(testTemplate, "Dossier1");
    }


    public Dossier provideTestDossier(DossierTemplateModel testTemplate, String dossierName) {

        TenantContext.setTenantId("redaction");
        return provideTestDossier(testTemplate, dossierName, null);
    }


    public Dossier provideTestDossier(DossierTemplateModel testTemplate, String dossierName, DossierStatusInfo dossierStatus) {

        var result = provideTestDossierQuick(testTemplate, dossierName, dossierStatus);

        assertThat(result.getDossierName()).isEqualTo(dossierName);

        Dossier loadedDossier = dossierClient.getDossier(result.getId(), false, false);

        assertThat(loadedDossier).isEqualTo(result);

        return loadedDossier;
    }


    public Dossier provideTestDossierQuick(DossierTemplateModel testTemplate, String dossierName, DossierStatusInfo dossierStatus) {

        log.info("Create dossier {}", dossierName);
        var cru = new DossierRequest();
        cru.setDownloadFileTypes(Sets.newHashSet(DownloadFileType.ORIGINAL));
        cru.setDossierName(dossierName);
        cru.setDescription(dossierName);
        if (dossierStatus != null) {
            cru.setDossierStatusId(dossierStatus.getId());
        }

        cru.setDueDate(OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS));
        cru.setDossierTemplateId(testTemplate.getId());
        cru.setReportTemplateIds(Set.of("reportTemplate1", "reportTemplate2"));

        var userId = userProvider.getUserId();
        var altUserId = userProvider.getAltUserId();
        cru.setOwnerId(userId);
        cru.setMemberIds(Sets.newHashSet(userId, altUserId));
        cru.setApproverIds(Sets.newHashSet(userId, altUserId));

        var result = dossierClient.createDossierOrUpdateDossier(cru);

        return result.getBody();
    }


    public Dossier provideTestDossier() {

        var testTemplate = dossierTemplateTesterAndProvider.provideTestTemplate();

        return provideTestDossier(testTemplate, "Dossier1");
    }


    public Dossier provideTestDossier(String dossierName) {

        var testTemplate = dossierTemplateTesterAndProvider.provideTestTemplate();

        return provideTestDossier(testTemplate, dossierName);
    }

}
