package com.njcn.gather.steady.datavie.service.impl;

import com.njcn.gather.steady.datavie.component.SteadyInfluxQueryComponent;
import com.njcn.gather.steady.datavie.component.SteadyTrendFieldResolver;
import com.njcn.gather.steady.datavie.component.SteadyTrendIndicatorCatalog;
import com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendResolvedFieldBO;
import com.njcn.gather.steady.datavie.pojo.param.SteadyTrendQueryParam;
import com.njcn.gather.steady.datavie.pojo.vo.SteadyTrendQueryVO;
import com.njcn.gather.tool.addledger.service.AddLedgerService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * 稳态趋势查询服务测试。
 */
class SteadyDataViewTrendServiceImplTest {

    @Test
    void shouldDisplayLineVoltagePhasesAsLinePairs() {
        SteadyInfluxQueryComponent influxQueryComponent = mock(SteadyInfluxQueryComponent.class);
        AddLedgerService addLedgerService = mock(AddLedgerService.class);
        SteadyDataViewTrendServiceImpl service = new SteadyDataViewTrendServiceImpl(
                new SteadyTrendFieldResolver(new SteadyTrendIndicatorCatalog()), influxQueryComponent, addLedgerService);
        when(addLedgerService.listLinePathByLineIds(eq(Collections.singletonList("line-001"))))
                .thenReturn(Collections.emptyMap());
        when(influxQueryComponent.queryTrendPoints(any(SteadyTrendResolvedFieldBO.class),
                any(LocalDateTime.class), any(LocalDateTime.class), eq(0))).thenReturn(Collections.emptyList());
        SteadyTrendQueryParam param = new SteadyTrendQueryParam();
        param.setLineIds(Collections.singletonList("line-001"));
        param.setIndicatorCodes(Collections.singletonList("V_LINE_RMS"));
        param.setStatTypes(Collections.singletonList("AVG"));
        param.setTimeStart("2026-05-01 00:00:00");
        param.setTimeEnd("2026-05-01 01:00:00");
        param.setQualityFlag(0);

        SteadyTrendQueryVO result = service.queryTrend(param);
        List<String> phases = result.getSeries().stream()
                .map(series -> series.getPhase())
                .collect(Collectors.toList());

        Assertions.assertEquals(Arrays.asList("AB", "BC", "CA"), phases);
    }
}
