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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
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.dto.OpenEarTagDto;
import com.ruoyi.web.modules.industryservice.domain.dto.OpenEnvMonitoringDto;
import com.ruoyi.web.modules.industryservice.support.ThirdPartyFarmingProperties;
import com.ruoyi.web.modules.industryservice.support.ThirdPartyIotEarTagClient;
import com.ruoyi.web.modules.industryservice.support.ThirdPartyIotEnvMonitoringClient;

@ExtendWith(MockitoExtension.class)
@DisplayName("YakAssetPhysioLiveService")
class YakAssetPhysioLiveServiceTest
{
    @Mock
    private ThirdPartyIotEarTagClient earTagClient;

    @Mock
    private ThirdPartyIotEnvMonitoringClient envMonitoringClient;

    @Mock
    private ThirdPartyFarmingProperties properties;

    @InjectMocks
    private YakAssetPhysioLiveService service;

    @Test
    @DisplayName("详情实时生理取自最新耳标与环境监测")
    void enrichRealtimePhysio()
    {
        when(properties.isEnabled()).thenReturn(true);
        OpenEarTagDto earTag = new OpenEarTagDto();
        earTag.setEarTagNumber("ET001");
        earTag.setTemperature("15.2");
        earTag.setSteps("1280");
        earTag.setReportTime("2026-05-20 09:15:00");
        when(earTagClient.fetchByEarTag(eq("ET001"), isNull(), isNull()))
                .thenReturn(Collections.singletonList(earTag));

        OpenEnvMonitoringDto env = new OpenEnvMonitoringDto();
        env.setFarmId(100L);
        env.setEnclosureId(200L);
        env.setTemperature(18.5);
        env.setMonitoringTime("2026-05-20 10:00:00");
        when(envMonitoringClient.fetchByFarmEnclosure(eq(100L), eq(200L), isNull(), isNull()))
                .thenReturn(Collections.singletonList(env));

        BizYakAsset asset = new BizYakAsset();
        asset.setEarTagNumber("ET001");
        asset.setFarmExternalId(100L);
        asset.setEnclosureExternalId(200L);
        service.enrichRealtimePhysio(asset);

        assertEquals(new BigDecimal("15.2"), asset.getRealtimeTemp());
        assertEquals(1280, asset.getRealtimeSteps());
        assertNotNull(asset.getPhysioCollectTime());
        assertEquals(new BigDecimal("18.5"), asset.getEnvTemp());
    }

    @Test
    @DisplayName("第三方未启用时不请求接口")
    void enrichWhenDisabled()
    {
        when(properties.isEnabled()).thenReturn(false);
        BizYakAsset asset = new BizYakAsset();
        asset.setEarTagNumber("ET001");
        service.enrichRealtimePhysio(asset);
        verify(properties).isEnabled();
    }
}
