package com.ruoyi.info.collection.controller;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.info.collection.domain.excel.CcdiAssetInfoExcel;
import com.ruoyi.info.collection.domain.vo.AssetImportFailureVO;
import com.ruoyi.info.collection.domain.vo.ImportResultVO;
import com.ruoyi.info.collection.domain.vo.ImportStatusVO;
import com.ruoyi.info.collection.service.ICcdiAssetInfoImportService;
import com.ruoyi.info.collection.utils.EasyExcelUtil;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockMultipartFile;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class CcdiAssetInfoControllerTest {

    @InjectMocks
    private CcdiAssetInfoController controller;

    @Mock
    private ICcdiAssetInfoImportService assetInfoImportService;

    @Test
    void importData_shouldReturnWarnWhenExcelHasNoRows() throws Exception {
        MockMultipartFile file = new MockMultipartFile(
                "file",
                "asset-empty.xlsx",
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                "empty".getBytes(StandardCharsets.UTF_8)
        );

        try (MockedStatic<EasyExcelUtil> mocked = mockStatic(EasyExcelUtil.class)) {
            mocked.when(() -> EasyExcelUtil.importExcel(any(InputStream.class), eq(CcdiAssetInfoExcel.class)))
                    .thenReturn(List.of());

            AjaxResult result = controller.importData(file);

            assertEquals(HttpStatus.WARN, result.get(AjaxResult.CODE_TAG));
            assertEquals("至少需要一条数据", result.get(AjaxResult.MSG_TAG));
        }
    }

    @Test
    void importData_shouldReturnSuccessWhenTaskCreated() throws Exception {
        MockMultipartFile file = new MockMultipartFile(
                "file",
                "asset.xlsx",
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                "asset".getBytes(StandardCharsets.UTF_8)
        );
        CcdiAssetInfoExcel excel = new CcdiAssetInfoExcel();
        excel.setPersonId("320101199001010011");
        when(assetInfoImportService.importAssetInfo(List.of(excel))).thenReturn("task-1");

        try (MockedStatic<EasyExcelUtil> mocked = mockStatic(EasyExcelUtil.class)) {
            mocked.when(() -> EasyExcelUtil.importExcel(any(InputStream.class), eq(CcdiAssetInfoExcel.class)))
                    .thenReturn(List.of(excel));

            AjaxResult result = controller.importData(file);

            assertEquals(HttpStatus.SUCCESS, result.get(AjaxResult.CODE_TAG));
            assertEquals("导入任务已提交,正在后台处理", result.get(AjaxResult.MSG_TAG));
            ImportResultVO data = (ImportResultVO) result.get(AjaxResult.DATA_TAG);
            assertEquals("task-1", data.getTaskId());
        }
    }

    @Test
    void getImportStatus_shouldDelegateToImportService() {
        ImportStatusVO statusVO = new ImportStatusVO();
        statusVO.setTaskId("task-2");
        when(assetInfoImportService.getImportStatus("task-2")).thenReturn(statusVO);

        AjaxResult result = controller.getImportStatus("task-2");

        assertEquals(HttpStatus.SUCCESS, result.get(AjaxResult.CODE_TAG));
        assertEquals(statusVO, result.get(AjaxResult.DATA_TAG));
    }

    @Test
    void getImportFailures_shouldReturnPagedRows() {
        AssetImportFailureVO failure1 = new AssetImportFailureVO();
        failure1.setSheetName("亲属资产信息");
        failure1.setRowNum(2);
        failure1.setPersonId("A1");
        AssetImportFailureVO failure2 = new AssetImportFailureVO();
        failure2.setSheetName("亲属资产信息");
        failure2.setRowNum(3);
        failure2.setPersonId("A2");
        when(assetInfoImportService.getImportFailures("task-3")).thenReturn(List.of(failure1, failure2));

        TableDataInfo result = controller.getImportFailures("task-3", 2, 1);

        assertEquals(2, result.getTotal());
        assertEquals(1, result.getRows().size());
        AssetImportFailureVO row = (AssetImportFailureVO) result.getRows().get(0);
        assertEquals("亲属资产信息", row.getSheetName());
        assertEquals(3, row.getRowNum());
        assertEquals("A2", row.getPersonId());
    }

    @Test
    void importTemplate_shouldUseFamilyAssetTemplateName() {
        try (MockedStatic<EasyExcelUtil> mocked = mockStatic(EasyExcelUtil.class)) {
            controller.importTemplate(null);

            mocked.verify(() -> EasyExcelUtil.importTemplateWithDictDropdown(null, CcdiAssetInfoExcel.class, "亲属资产信息"));
        }
    }
}
