package ru.nbch.indicsearchservice.exporter;

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

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ru.nbch.indicsearchservice.config.settings.ApplicationSettings;
import ru.nbch.indicsearchservice.model.TableMetaData;
import ru.nbch.indicsearchservice.model.enums.SearchParameter;

@SpringBootTest
class JsonExporterTest {
    @Autowired
    ApplicationSettings settings;
    @Autowired
    JsonExporter jsonExporter;

    @BeforeEach
    void setUp() {
        settings.setStorageDirectory(Path.of("src/test/resources/storage").toFile());
    }

    @Test
    void writeJsonFile_shouldCreateJsonFilePASSPORT() throws IOException {
        Path aggregatedFile = settings.getStorageDirectory().toPath().resolve("test-json-aggregated.csv");
        UUID uuid = UUID.fromString("c3805074-e840-4d4e-bfa3-eee1fefd4907");

        TableMetaData ruUidTableTableMetaData = new TableMetaData("RU_UID");
        ruUidTableTableMetaData.addField("FID");
        ruUidTableTableMetaData.addField("SERIAL_NUM");
        TableMetaData ruApplicationTableTableMetaData = new TableMetaData("RU_APPLICATION");
        ruApplicationTableTableMetaData.addField("FID");
        ruApplicationTableTableMetaData.addField("SERIAL_NUM");

        Map<String, TableMetaData> tableMetaDataMap = new HashMap<>();
        tableMetaDataMap.put("RU_UID", ruUidTableTableMetaData);
        tableMetaDataMap.put("RU_APPLICATION", ruApplicationTableTableMetaData);

        jsonExporter.writeJsonFile(aggregatedFile, uuid, SearchParameter.PASSPORT, tableMetaDataMap);

        Path resultFile = settings.getStorageDirectory().toPath().resolve("c3805074-e840-4d4e-bfa3-eee1fefd4907.json");
        List<String> resultFileStrings = Files.readAllLines(resultFile);

        Files.deleteIfExists(resultFile);

        assertThat(resultFileStrings.getFirst())
            .isEqualTo("{\"datasheets\":[[{\"table_name\":\"RU_UID\",\"field_data\":[{\"key\":\"FID\",\"value\":\"1\"},{\"key\":\"SERIAL_NUM\",\"value\":\"11\"}],\"related_data\":[]},{\"table_name\":\"RU_UID\",\"field_data\":[{\"key\":\"FID\",\"value\":\"1\"},{\"key\":\"SERIAL_NUM\",\"value\":\"21\"}],\"related_data\":[{\"table_name\":\"RU_TRADE\",\"field_data\":[],\"related_data\":[]}]}],[{\"table_name\":\"RU_APPLICATION\",\"field_data\":[{\"key\":\"FID\",\"value\":\"2\"},{\"key\":\"SERIAL_NUM\",\"value\":\"12\"}],\"related_data\":[]}],[{\"table_name\":\"RU_APPLICATION\",\"field_data\":[{\"key\":\"FID\",\"value\":\"3\"},{\"key\":\"SERIAL_NUM\",\"value\":\"22\"}],\"related_data\":[]}]]}");
    }

    @Test
    void writeJsonFile_shouldCreateJsonFileUUID() throws IOException {
        Path aggregatedFile = settings.getStorageDirectory().toPath().resolve("test-json-aggregated.csv");
        UUID uuid = UUID.fromString("c3805074-e840-4d4e-bfa3-eee1fefd4907");

        TableMetaData ruUidTableTableMetaData = new TableMetaData("RU_UID");
        ruUidTableTableMetaData.addField("FID");
        ruUidTableTableMetaData.addField("SERIAL_NUM");
        TableMetaData ruApplicationTableTableMetaData = new TableMetaData("RU_APPLICATION");
        ruApplicationTableTableMetaData.addField("FID");
        ruApplicationTableTableMetaData.addField("SERIAL_NUM");

        Map<String, TableMetaData> tableMetaDataMap = new HashMap<>();
        tableMetaDataMap.put("RU_UID", ruUidTableTableMetaData);
        tableMetaDataMap.put("RU_APPLICATION", ruApplicationTableTableMetaData);

        jsonExporter.writeJsonFile(aggregatedFile, uuid, SearchParameter.UUID, tableMetaDataMap);

        Path resultFile = settings.getStorageDirectory().toPath().resolve("c3805074-e840-4d4e-bfa3-eee1fefd4907.json");
        List<String> resultFileStrings = Files.readAllLines(resultFile);

        Files.deleteIfExists(resultFile);

        assertThat(resultFileStrings.getFirst())
            .isEqualTo("{\"datasheets\":[[{\"table_name\":\"RU_UID\",\"field_data\":[{\"key\":\"FID\",\"value\":\"1\"},{\"key\":\"SERIAL_NUM\",\"value\":\"11\"}],\"related_data\":[]}],[{\"table_name\":\"RU_UID\",\"field_data\":[{\"key\":\"FID\",\"value\":\"1\"},{\"key\":\"SERIAL_NUM\",\"value\":\"21\"}],\"related_data\":[{\"table_name\":\"RU_TRADE\",\"field_data\":[],\"related_data\":[]}]}],[{\"table_name\":\"RU_APPLICATION\",\"field_data\":[{\"key\":\"FID\",\"value\":\"2\"},{\"key\":\"SERIAL_NUM\",\"value\":\"12\"}],\"related_data\":[]}],[{\"table_name\":\"RU_APPLICATION\",\"field_data\":[{\"key\":\"FID\",\"value\":\"3\"},{\"key\":\"SERIAL_NUM\",\"value\":\"22\"}],\"related_data\":[]}]]}");
    }
}