package com.gxwebsoft.credit.controller;

import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.credit.entity.CreditBankruptcy;
import com.gxwebsoft.credit.service.CreditBankruptcyService;
import com.gxwebsoft.credit.service.CreditCompanyRecordCountService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class CreditBankruptcyImportSheetSelectionTest {

    @Test
    void import_should_prefer_sheet_named_bankruptcy_reorganization() throws Exception {
        MultipartFile file = buildWorkbookWithTwoSheets();

        CreditBankruptcyController controller = new CreditBankruptcyController();

        CreditBankruptcyService creditBankruptcyService = Mockito.mock(CreditBankruptcyService.class);
        BatchImportSupport batchImportSupport = Mockito.mock(BatchImportSupport.class);
        CreditCompanyRecordCountService recordCountService = Mockito.mock(CreditCompanyRecordCountService.class);

        // Capture inserted entities; controller clears the chunk list after calling persistInsertOnlyChunk.
        List<CreditBankruptcy> inserted = new ArrayList<>();
        Mockito.when(batchImportSupport.persistInsertOnlyChunk(
                Mockito.eq(creditBankruptcyService),
                Mockito.anyList(),
                Mockito.anyList(),
                Mockito.anyInt(),
                Mockito.any(),
                Mockito.anyString(),
                Mockito.anyList()
            ))
            .thenAnswer(invocation -> {
                @SuppressWarnings("unchecked")
                List<CreditBankruptcy> items = new ArrayList<>((List<CreditBankruptcy>) invocation.getArgument(1));
                inserted.addAll(items);
                return items.size();
            });

        ReflectionTestUtils.setField(controller, "creditBankruptcyService", creditBankruptcyService);
        ReflectionTestUtils.setField(controller, "batchImportSupport", batchImportSupport);
        ReflectionTestUtils.setField(controller, "creditCompanyRecordCountService", recordCountService);

        ApiResult<List<String>> result = controller.importBatch(file, null);

        assertNotNull(result);
        assertEquals(0, result.getCode());
        assertTrue(result.getMessage().contains("成功导入1条"), "message=" + result.getMessage());
        assertEquals(1, inserted.size());
        // "历史破产重整" sheet is first; we should import from "破产重整" instead.
        assertEquals("R1", inserted.get(0).getCode());
    }

    private static MultipartFile buildWorkbookWithTwoSheets() throws Exception {
        try (Workbook workbook = new XSSFWorkbook()) {
            // Put "历史破产重整" first to ensure old readAnySheet() behavior would import the wrong sheet.
            writeBankruptcySheet(workbook.createSheet("历史破产重整"), "H1");
            writeBankruptcySheet(workbook.createSheet("破产重整"), "R1");

            try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
                workbook.write(bos);
                return new MockMultipartFile(
                    "file",
                    "bankruptcy.xlsx",
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                    bos.toByteArray()
                );
            }
        }
    }

    private static void writeBankruptcySheet(Sheet sheet, String code) {
        // Keep it simple: a single header row + single data row.
        Row header = sheet.createRow(0);
        header.createCell(0).setCellValue("案号");
        header.createCell(1).setCellValue("案件类型");
        header.createCell(2).setCellValue("当事人");
        header.createCell(3).setCellValue("经办法院");
        header.createCell(4).setCellValue("公开日期");
        header.createCell(5).setCellValue("备注");

        Row row = sheet.createRow(1);
        row.createCell(0).setCellValue(code);
        row.createCell(1).setCellValue("破产重整");
        row.createCell(2).setCellValue("示例公司");
        row.createCell(3).setCellValue("示例法院");
        row.createCell(4).setCellValue("2026-01-01");
        row.createCell(5).setCellValue("备注");
    }
}
