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

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

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

/**
 * add-data 生成值契约测试。
 */
class AddDataValueGeneratorTest {

    @Test
    void shouldMarkGeneratedDataAsValidByDefault() {
        AddDataValueGenerator generator = new AddDataValueGenerator();
        AddDataTableDefinition definition = new AddDataTableDefinition("data_v",
                Arrays.asList("TIMEID", "LINEID", "PHASIC_TYPE", "QUALITYFLAG", "RMS"),
                Arrays.asList("A"), 100, AddDataTableDefinition.TimeAxisType.REQUEST_INTERVAL);

        List<Object> row = generator.generateRow(definition, "line-001",
                LocalDateTime.of(2026, 5, 18, 10, 0, 0), "A");

        Assertions.assertEquals(0, row.get(3));
    }

    @Test
    void shouldKeepStatValuesOrderedForSameTimeAndMetric() {
        AddDataValueGenerator generator = new AddDataValueGenerator();
        AddDataTableDefinition definition = new AddDataTableDefinition("data_v",
                Arrays.asList("TIMEID", "LINEID", "PHASIC_TYPE", "QUALITYFLAG",
                        "RMS", "RMS_MAX", "RMS_CP95", "RMS_MIN"),
                Arrays.asList("A"), 100, AddDataTableDefinition.TimeAxisType.REQUEST_INTERVAL);

        List<Object> row = generator.generateRow(definition, "line-001",
                LocalDateTime.of(2026, 5, 18, 10, 0, 0), "A");

        double avg = ((Number) row.get(4)).doubleValue();
        double max = ((Number) row.get(5)).doubleValue();
        double cp95 = ((Number) row.get(6)).doubleValue();
        double min = ((Number) row.get(7)).doubleValue();
        Assertions.assertTrue(max >= cp95, "MAX should be greater than or equal to CP95");
        Assertions.assertTrue(cp95 >= avg, "CP95 should be greater than or equal to AVG");
        Assertions.assertTrue(avg >= min, "AVG should be greater than or equal to MIN");
    }
}
