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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.industryservice.domain.BizYakOutboundReport;

@ExtendWith(MockitoExtension.class)
@DisplayName("YakOutboundReportValidation")
class YakOutboundReportValidationTest
{
    @Mock
    private TownDeptSupport townDeptSupport;

    @Test
    @DisplayName("ZCZX-MYCNCL-UT-003 缺村")
    void ut003_missingVillage()
    {
        BizYakOutboundReport row = sampleRow();
        row.setVillageDeptId(null);
        assertThrows(ServiceException.class, () -> YakOutboundReportValidation.validateRowForSave(row, townDeptSupport));
    }

    @Test
    @DisplayName("ZCZX-MYCNCL-UT-004 负数拒绝")
    void ut004_negative()
    {
        mockVillage();
        BizYakOutboundReport row = sampleRow();
        row.setYakOutboundCount(-1);
        assertThrows(ServiceException.class, () -> YakOutboundReportValidation.validateRowForSave(row, townDeptSupport));
    }

    @Test
    @DisplayName("ZCZX-MYCNCL-UT-010 扎色镇别名")
    void ut010_normalizeTownName()
    {
        assertEquals("杂色镇", TownDeptSupport.normalizeTownName("扎色镇"));
        assertEquals("巴青乡", TownDeptSupport.normalizeTownName("巴青乡"));
    }

    @Test
    @DisplayName("ZCZX-MYCNCL-UT-018 自食大于出栏警告")
    void ut018_selfConsumptionWarn()
    {
        BizYakOutboundReport row = sampleRow();
        row.setYakOutboundCount(10);
        row.setCattleOutboundCount(0);
        row.setSelfConsumptionCattleCount(20);
        String warn = YakOutboundReportValidation.buildSelfConsumptionWarn(row);
        assertTrue(warn != null && warn.contains("自食牛"));
    }

    private void mockVillage()
    {
        SysDept village = new SysDept();
        village.setDeptId(201L);
        village.setDeptName("荣嘎村");
        village.setParentId(101L);
        when(townDeptSupport.requireReportVillage(201L)).thenReturn(village);
        SysDept town = new SysDept();
        town.setDeptId(101L);
        town.setDeptName("雅安镇");
        when(townDeptSupport.requireReportTown(101L)).thenReturn(town);
    }

    private static BizYakOutboundReport sampleRow()
    {
        BizYakOutboundReport row = new BizYakOutboundReport();
        row.setStatYear(2026);
        row.setStatMonth(5);
        row.setVillageDeptId(201L);
        return row;
    }
}
