package com.njcn.gather.tool.adddata.component;

import com.njcn.gather.tool.adddata.pojo.bo.AddDataInfluxFieldGroup;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * add-data InfluxDB 字段映射契约测试。
 */
class AddDataInfluxFieldMapperTest {

    @Test
    void shouldSplitStatsIntoValueTypeGroups() {
        AddDataInfluxFieldMapper mapper = new AddDataInfluxFieldMapper();
        List<String> columns = Arrays.asList("TIMEID", "LINEID", "PHASIC_TYPE", "QUALITYFLAG",
                "RMS", "RMS_MAX", "RMS_MIN", "RMS_CP95");
        List<Object> row = Arrays.<Object>asList(Timestamp.valueOf("2026-05-18 10:00:00"),
                "line-001", "A", 0, 220.1D, 225.2D, 218.3D, 224.4D);

        List<AddDataInfluxFieldGroup> groups = mapper.mapFieldGroups("data_v", columns, row);

        Assertions.assertEquals(4, groups.size());
        Assertions.assertEquals("AVG", groups.get(0).getValueType());
        Assertions.assertEquals(220.1D, groups.get(0).getFields().get("rms"));
        Assertions.assertEquals("MAX", groups.get(1).getValueType());
        Assertions.assertEquals(225.2D, groups.get(1).getFields().get("rms"));
        Assertions.assertEquals("MIN", groups.get(2).getValueType());
        Assertions.assertEquals(218.3D, groups.get(2).getFields().get("rms"));
        Assertions.assertEquals("CP95", groups.get(3).getValueType());
        Assertions.assertEquals(224.4D, groups.get(3).getFields().get("rms"));
    }

    @Test
    void shouldSkipValueTypeForFlickerMeasurements() {
        AddDataInfluxFieldMapper mapper = new AddDataInfluxFieldMapper();
        List<String> columns = Arrays.asList("TIMEID", "LINEID", "PHASIC_TYPE", "QUALITYFLAG", "FLUC", "FLUCCF");
        List<Object> row = Arrays.<Object>asList(Timestamp.valueOf("2026-05-18 10:00:00"),
                "line-001", "T", 0, 0.31D, 0.29D);

        List<AddDataInfluxFieldGroup> groups = mapper.mapFieldGroups("data_fluc", columns, row);

        Assertions.assertEquals(1, groups.size());
        Assertions.assertNull(groups.get(0).getValueType());
        Assertions.assertEquals(0.31D, groups.get(0).getFields().get("fluc"));
        Assertions.assertEquals(0.29D, groups.get(0).getFields().get("fluccf"));
    }

    @Test
    void shouldMapInfluxTimestampFromTimeId() {
        AddDataInfluxFieldMapper mapper = new AddDataInfluxFieldMapper();
        LocalDateTime time = mapper.resolveTimeId(Arrays.asList("TIMEID"), Arrays.<Object>asList(Timestamp.valueOf("2026-05-18 10:00:00")));

        Assertions.assertEquals(LocalDateTime.of(2026, 5, 18, 10, 0, 0), time);
    }
}
