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

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.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.BizThreeAssetsProblemReport;

@ExtendWith(MockitoExtension.class)
@DisplayName("ThreeAssetsProblemReportValidation")
class ThreeAssetsProblemReportValidationTest
{
    @Mock
    private TownDeptSupport townDeptSupport;

    @Test
    @DisplayName("保存时反填乡镇并默认上报时间")
    void fillTownAndDefaultReportTime()
    {
        SysDept town = town(101L, "雅安镇");
        SysDept village = village(201L, "荣嘎村", 101L);
        when(townDeptSupport.requireReportVillage(201L)).thenReturn(village);
        when(townDeptSupport.requireReportTown(101L)).thenReturn(town);

        BizThreeAssetsProblemReport row = new BizThreeAssetsProblemReport();
        row.setVillageDeptId(201L);
        row.setProblemLevel(ThreeAssetsProblemReportRules.LEVEL_GENERAL);
        row.setProblemDetail("资金账目不清");
        ThreeAssetsProblemReportValidation.validateRowForSave(row, townDeptSupport);

        assertEquals(101L, row.getTownDeptId());
        assertEquals("雅安镇", row.getTownName());
        assertEquals("荣嘎村", row.getVillageName());
        assertNotNull(row.getReportTime());
    }

    @Test
    @DisplayName("问题等级不合法时拒绝")
    void invalidLevelRejected()
    {
        SysDept town = town(101L, "雅安镇");
        SysDept village = village(201L, "荣嘎村", 101L);
        when(townDeptSupport.requireReportVillage(201L)).thenReturn(village);
        when(townDeptSupport.requireReportTown(101L)).thenReturn(town);

        BizThreeAssetsProblemReport row = new BizThreeAssetsProblemReport();
        row.setVillageDeptId(201L);
        row.setProblemLevel("unknown");
        row.setProblemDetail("描述");
        assertThrows(ServiceException.class,
                () -> ThreeAssetsProblemReportValidation.validateRowForSave(row, townDeptSupport));
    }

    private static SysDept town(Long id, String name)
    {
        SysDept d = new SysDept();
        d.setDeptId(id);
        d.setDeptName(name);
        d.setParentId(YakHerdInventoryRules.TOWN_PARENT_ID);
        d.setOrderNum(20);
        d.setStatus("0");
        d.setDelFlag("0");
        return d;
    }

    private static SysDept village(Long id, String name, Long townId)
    {
        SysDept d = new SysDept();
        d.setDeptId(id);
        d.setDeptName(name);
        d.setParentId(townId);
        d.setStatus("0");
        d.setDelFlag("0");
        return d;
    }
}
