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.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import java.util.Arrays;
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.common.exception.ServiceException;
import com.ruoyi.web.modules.industryservice.domain.dto.OpenFarmDto;
import com.ruoyi.web.modules.industryservice.domain.vo.YakAssetSyncResultVo;
import com.ruoyi.web.modules.industryservice.support.PastureSyncRules;
import com.ruoyi.web.modules.industryservice.support.ThirdPartyFarmClient;

@ExtendWith(MockitoExtension.class)
@DisplayName("牧场第三方同步")
class PastureSyncServiceTest
{
    @Mock
    private ThirdPartyFarmClient thirdPartyFarmClient;

    @Mock
    private PastureSyncTxService pastureSyncTxService;

    @InjectMocks
    private PastureSyncService pastureSyncService;

    @Test
    @DisplayName("第三方未启用")
    void syncDisabled()
    {
        when(thirdPartyFarmClient.fetchAllFarms()).thenThrow(new ServiceException("第三方养殖场同步未启用"));
        assertThrows(ServiceException.class, () -> pastureSyncService.syncFromThirdParty());
    }

    @Test
    @DisplayName("空列表同步")
    void syncEmpty()
    {
        when(thirdPartyFarmClient.fetchAllFarms()).thenReturn(Collections.emptyList());
        YakAssetSyncResultVo result = pastureSyncService.syncFromThirdParty();
        assertNotNull(result.getSyncTime());
        assertEquals(0, result.getInsertCount());
    }

    @Test
    @DisplayName("新增与更新统计")
    void syncInsertAndUpdate()
    {
        OpenFarmDto farm1 = new OpenFarmDto();
        farm1.setId(1L);
        farm1.setFarmName("牧场A");
        OpenFarmDto farm2 = new OpenFarmDto();
        farm2.setId(2L);
        farm2.setFarmName("牧场B");
        when(thirdPartyFarmClient.fetchAllFarms()).thenReturn(Arrays.asList(farm1, farm2));
        when(pastureSyncTxService.persistFarm(any(), any())).thenReturn(true, false);
        YakAssetSyncResultVo result = pastureSyncService.syncFromThirdParty();
        assertEquals(1, result.getInsertCount());
        assertEquals(1, result.getUpdateCount());
    }

    @Test
    @DisplayName("并发同步拒绝")
    void syncInProgress() throws Exception
    {
        OpenFarmDto farm = new OpenFarmDto();
        farm.setId(1L);
        farm.setFarmName("牧场A");
        when(thirdPartyFarmClient.fetchAllFarms()).thenReturn(Collections.singletonList(farm));
        when(pastureSyncTxService.persistFarm(any(), any())).thenAnswer(invocation -> {
            Thread.sleep(200);
            return true;
        });
        Thread t1 = new Thread(() -> pastureSyncService.syncFromThirdParty());
        t1.start();
        Thread.sleep(50);
        ServiceException ex = assertThrows(ServiceException.class, () -> pastureSyncService.syncFromThirdParty());
        assertEquals(PastureSyncRules.MSG_SYNC_IN_PROGRESS, ex.getMessage());
        t1.join();
    }
}
