package com.ruoyi.web.modules.industryservice.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.math.BigDecimal;
import java.util.Collections;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import com.ruoyi.web.modules.industryservice.domain.BizYakAsset;
import com.ruoyi.web.modules.industryservice.domain.BizYakGrowthRecord;
import com.ruoyi.web.modules.industryservice.domain.dto.OpenWeighingDto;
import com.ruoyi.web.modules.industryservice.domain.vo.YakAssetGrowthChartVo;
import com.ruoyi.web.modules.industryservice.mapper.BizYakGrowthRecordMapper;
import com.ruoyi.web.modules.industryservice.support.ThirdPartyFarmingProperties;
import com.ruoyi.web.modules.industryservice.support.ThirdPartyIotWeighingClient;
import com.ruoyi.web.modules.industryservice.support.YakAssetRules;

@ExtendWith(MockitoExtension.class)
@DisplayName("YakAssetGrowthLiveService")
class YakAssetGrowthLiveServiceTest
{
    @Mock
    private ThirdPartyIotWeighingClient weighingClient;

    @Mock
    private BizYakGrowthRecordMapper growthRecordMapper;

    @Mock
    private ThirdPartyFarmingProperties properties;

    @InjectMocks
    private YakAssetGrowthLiveService service;

    @Test
    @DisplayName("近一年称重转生长列表")
    void fetchGrowthRecords()
    {
        when(properties.isEnabled()).thenReturn(true);
        OpenWeighingDto dto = new OpenWeighingDto();
        dto.setWeight("300");
        dto.setWeighingTime("2026-05-20 08:30:00");
        when(weighingClient.fetchByCattleId(eq(4001L), any(), any())).thenReturn(Collections.singletonList(dto));

        BizYakAsset asset = new BizYakAsset();
        asset.setExternalId(4001L);
        assertEquals(1, service.fetchGrowthRecords(asset).size());
        verify(weighingClient).fetchByCattleId(eq(4001L), any(), any());
    }

    @Test
    @DisplayName("体高曲线读本地生长子表")
    void fetchGrowthChartHeight()
    {
        BizYakGrowthRecord row = new BizYakGrowthRecord();
        row.setHeightCm(new BigDecimal("95"));
        row.setDataKind(YakAssetRules.DATA_KIND_ACTUAL);
        when(growthRecordMapper.selectByYakAssetId(1L)).thenReturn(Collections.singletonList(row));

        BizYakAsset asset = new BizYakAsset();
        asset.setId(1L);
        YakAssetGrowthChartVo chart = service.fetchGrowthChart(asset, "height");
        assertEquals(1, chart.getActual().size());
    }
}
