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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.iqser.red.service.persistence.service.v1.api.shared.model.analysislog.entitylog.EntityLog;
import com.iqser.red.service.persistence.service.v1.api.shared.mongo.document.EntityLogDocument;
import com.iqser.red.service.persistence.service.v1.api.shared.mongo.document.EntityLogEntryDocument;
import com.iqser.red.service.persistence.service.v1.api.shared.mongo.mapper.EntityLogDocumentMapper;

import lombok.SneakyThrows;

public class EntityLogDocumentMapperTest {

    private final EntityLogDocumentMapper mapper = EntityLogDocumentMapper.INSTANCE;

    private final String ENTITY_LOG = "files/entity-log/b2cbdd4dca0aa1aa0ebbfc5cc1462df0.ENTITY_LOG.json";
    private static final String TEST_DOSSIER_ID = "91ce8e90-9aec-473c-b8c3-cbe16443ad34";
    private static final String TEST_FILE_ID = "b2cbdd4dca0aa1aa0ebbfc5cc1462df0";


    @Test
    @SneakyThrows
    public void testEntityLogMapper() {

        var file = new ClassPathResource(String.format(ENTITY_LOG));
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());

        EntityLog entityLogBefore = objectMapper.readValue(file.getInputStream(), EntityLog.class);

        EntityLogDocument entityLogDocument = mapper.toLogDocument(TEST_DOSSIER_ID, TEST_FILE_ID, entityLogBefore);

        assertEquals(entityLogDocument.getDossierId(), TEST_DOSSIER_ID);
        assertEquals(entityLogDocument.getFileId(), TEST_FILE_ID);
        assertEquals(entityLogDocument.getId(), mapper.getLogId(TEST_DOSSIER_ID, TEST_FILE_ID));

        Optional<EntityLogEntryDocument> optionalEntityLogEntryDocument = entityLogDocument.getEntityLogEntryDocuments()
                .stream()
                .findFirst();

        assertTrue(optionalEntityLogEntryDocument.isPresent());
        assertNotNull(optionalEntityLogEntryDocument.get().getEntityLogId());
        assertNotNull(optionalEntityLogEntryDocument.get().getEntryId());
        assertEquals(optionalEntityLogEntryDocument.get().getId(), mapper.getLogEntryId(TEST_DOSSIER_ID, TEST_FILE_ID, optionalEntityLogEntryDocument.get().getEntryId()));

        EntityLog entityLogAfter = mapper.fromLogDocument(entityLogDocument);

        assertEquals(entityLogBefore, entityLogAfter);

    }

}
