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.anyList;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.TimeZone;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import com.ruoyi.web.modules.industryservice.domain.BizYakHealthSourceRecord;
import com.ruoyi.web.modules.industryservice.domain.dto.YakDiseaseWarningDto;
import com.ruoyi.web.modules.industryservice.mapper.BizYakHealthSourceRecordMapper;
import com.ruoyi.web.modules.industryservice.support.YakDiseaseWarningAnalysisRules;

@ExtendWith(MockitoExtension.class)
@DisplayName("YakDiseaseWarningAnalyzer")
class YakDiseaseWarningAnalyzerTest
{
    @Mock
    private BizYakHealthSourceRecordMapper healthSourceRecordMapper;

    @Mock
    private YakDiseaseWarningSyncTxService warningSyncTxService;

    @InjectMocks
    private YakDiseaseWarningAnalyzer analyzer;

    @Test
    @DisplayName("检疫同地区同日异常集中生成预警")
    void quarantineConcentrationCreatesWarning() throws Exception
    {
        Date recordTime = parseTime("2026-05-19 10:00:00");
        BizYakHealthSourceRecord r1 = quarantineRow(1L, recordTime, "阳性");
        BizYakHealthSourceRecord r2 = quarantineRow(2L, recordTime, "异常");
        BizYakHealthSourceRecord r3 = quarantineRow(3L, recordTime, "阳性");
        when(healthSourceRecordMapper.selectUnanalyzedByType(YakDiseaseWarningAnalysisRules.RECORD_TYPE_QUARANTINE))
                .thenReturn(Arrays.asList(r1, r2, r3));
        when(healthSourceRecordMapper.selectUnanalyzedByType(YakDiseaseWarningAnalysisRules.RECORD_TYPE_DIAGNOSIS))
                .thenReturn(java.util.Collections.emptyList());
        when(healthSourceRecordMapper.selectUnanalyzedByType(YakDiseaseWarningAnalysisRules.RECORD_TYPE_BREEDING))
                .thenReturn(java.util.Collections.emptyList());
        when(warningSyncTxService.persistAlert(any(YakDiseaseWarningDto.class), any(Date.class))).thenReturn(true);

        int created = analyzer.analyzeAndGenerate(new Date());
        assertEquals(1, created);

        ArgumentCaptor<YakDiseaseWarningDto> captor = ArgumentCaptor.forClass(YakDiseaseWarningDto.class);
        verify(warningSyncTxService).persistAlert(captor.capture(), any(Date.class));
        assertEquals(YakDiseaseWarningAnalysisRules.WARNING_TYPE_QUARANTINE, captor.getValue().getWarningType());
        verify(healthSourceRecordMapper).markAnalyzedByIds(anyList());
    }

    private static BizYakHealthSourceRecord quarantineRow(Long id, Date recordTime, String result)
    {
        BizYakHealthSourceRecord row = new BizYakHealthSourceRecord();
        row.setId(id);
        row.setRecordType(YakDiseaseWarningAnalysisRules.RECORD_TYPE_QUARANTINE);
        row.setRegionKey("F:1");
        row.setFarmName("示范牧场");
        row.setYakNo("YAK-Q-" + id);
        row.setRecordTime(recordTime);
        row.setQuarantineResult(result);
        return row;
    }

    private static Date parseTime(String text) throws Exception
    {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        df.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        return df.parse(text);
    }
}
