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.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.TestPropertySource;

import com.iqser.red.service.peristence.v1.server.integration.utils.AbstractPersistenceServerServiceTest;
import com.iqser.red.service.persistence.management.v1.processor.dataexchange.service.DossierTemplateImportService;
import com.iqser.red.service.persistence.management.v1.processor.entity.configuration.DateFormatsEntity;
import com.iqser.red.service.persistence.management.v1.processor.migration.migrations.V25AddDateFormatsToTemplatesMigration;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.DateFormatsPersistenceService;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.importexport.ImportDossierTemplateRequest;
import com.knecon.fforesight.tenantcommons.TenantApplicationType;

import lombok.SneakyThrows;

@TestPropertySource(properties = {"application.type=DocuMine"})
public class V25AddDateFormatsToTemplatesMigrationTest extends AbstractPersistenceServerServiceTest {

    private static final String USER_ID = "TestUserId";

    @Autowired
    private V25AddDateFormatsToTemplatesMigration migration;

    @Autowired
    private DossierTemplateImportService dossierTemplateImportService;

    @Autowired
    private DateFormatsPersistenceService dateFormatsPersistenceService;


    @Override
    protected TenantApplicationType getTenantApplicationType() {

        return TenantApplicationType.DocuMine;
    }


    @Test
    @SneakyThrows
    public void testAddDateFormatsMigration() {

        Path floraTemplatePath = new ClassPathResource("files/dossiertemplates/Flora_old.zip").getFile().toPath();
        byte[] floraTemplateBytes = Files.readAllBytes(floraTemplatePath);

        ImportDossierTemplateRequest importRequest = ImportDossierTemplateRequest.builder().archive(floraTemplateBytes).userId(USER_ID).build();

        String dossierTemplateId = dossierTemplateImportService.importDossierTemplate(importRequest);
        assertNotNull(dossierTemplateId);

        migration.run(true);

        Optional<DateFormatsEntity> dateFormatsOptional = dateFormatsPersistenceService.getDateFormats(dossierTemplateId);
        assertTrue(dateFormatsOptional.isPresent());

        DateFormatsEntity dateFormatsEntity = dateFormatsOptional.get();

        String expectedDateFormats = readDateFormatsFromClasspath();

        assertEquals(expectedDateFormats, dateFormatsEntity.getValue());
    }


    private String readDateFormatsFromClasspath() throws Exception {

        ClassPathResource resource = new ClassPathResource("files/dateFormats.txt");
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) {
            return reader.lines()
                    .collect(Collectors.joining(System.lineSeparator()));
        }
    }

}
