package com.ruoyi.ccdi.project.controller;

import com.ruoyi.ccdi.project.domain.dto.CcdiPullBankInfoSubmitDTO;
import com.ruoyi.ccdi.project.service.ICcdiFileUploadService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
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.nio.charset.StandardCharsets;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class CcdiFileUploadControllerTest {

    private static final Long PROJECT_ID = 100L;

    @InjectMocks
    private CcdiFileUploadController controller;

    @Mock
    private ICcdiFileUploadService fileUploadService;

    @Test
    void parseIdCardFile_shouldReturnAjaxResultSuccess() {
        MockMultipartFile file = new MockMultipartFile(
            "file",
            "ids.xlsx",
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
            "test".getBytes(StandardCharsets.UTF_8)
        );
        when(fileUploadService.parseIdCardFile(file)).thenReturn(List.of("110101199001018888"));

        AjaxResult result = controller.parseIdCardFile(file);

        assertEquals(200, result.get("code"));
    }

    @Test
    void pullBankInfo_shouldUseCurrentLoginUserInfo() {
        CcdiPullBankInfoSubmitDTO dto = new CcdiPullBankInfoSubmitDTO();
        dto.setProjectId(PROJECT_ID);
        dto.setIdCards(List.of("110101199001018888"));
        dto.setStartDate("2026-03-01");
        dto.setEndDate("2026-03-10");

        try (MockedStatic<SecurityUtils> mocked = mockStatic(SecurityUtils.class)) {
            mocked.when(SecurityUtils::getUserId).thenReturn(9527L);
            mocked.when(SecurityUtils::getUsername).thenReturn("admin");
            when(fileUploadService.submitPullBankInfo(PROJECT_ID, dto.getIdCards(), "2026-03-01", "2026-03-10", 9527L, "admin"))
                .thenReturn("batch-1");

            AjaxResult result = controller.pullBankInfo(dto);

            assertEquals(200, result.get("code"));
        }
    }

    @Test
    void deleteFile_shouldUseCurrentLoginUserId() {
        try (MockedStatic<SecurityUtils> mocked = mockStatic(SecurityUtils.class)) {
            mocked.when(SecurityUtils::getUserId).thenReturn(9527L);
            when(fileUploadService.deleteFileUploadRecord(123L, 9527L))
                .thenReturn("删除成功");

            AjaxResult result = controller.deleteFile(123L);

            assertEquals(200, result.get("code"));
            assertEquals("删除成功", result.get("msg"));
            verify(fileUploadService).deleteFileUploadRecord(123L, 9527L);
        }
    }
}
