package com.ruoyi.info.collection.service;

import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;

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

class CcdiStaffRecruitmentDualImportContractTest {

    @Test
    void shouldExposeSingleDualSheetImportEntry() throws Exception {
        String controller = Files.readString(
            Path.of("src/main/java/com/ruoyi/info/collection/controller/CcdiStaffRecruitmentController.java")
        );
        assertTrue(controller.contains("\"招聘信息\""));
        assertTrue(controller.contains("\"历史工作经历\""));
        assertFalse(controller.contains("workImportTemplate"));
        assertFalse(controller.contains("importWorkData"));

        String service = Files.readString(
            Path.of("src/main/java/com/ruoyi/info/collection/service/ICcdiStaffRecruitmentService.java")
        );
        assertTrue(service.contains("String importRecruitment("));
        assertTrue(service.contains("List<CcdiStaffRecruitmentExcel> recruitmentList"));
        assertTrue(service.contains("List<CcdiStaffRecruitmentWorkExcel> workList"));
        assertFalse(service.contains("importRecruitmentWork("));

        String importService = Files.readString(
            Path.of("src/main/java/com/ruoyi/info/collection/service/ICcdiStaffRecruitmentImportService.java")
        );
        assertTrue(importService.contains("void importRecruitmentAsync("));
        assertTrue(importService.contains("List<CcdiStaffRecruitmentExcel> recruitmentList"));
        assertTrue(importService.contains("List<CcdiStaffRecruitmentWorkExcel> workList"));
        assertFalse(importService.contains("importRecruitmentWorkAsync("));
    }

    @Test
    void shouldExposeFailureSheetFieldsAndSingleTaskInit() throws Exception {
        assertHasField(
            "com.ruoyi.info.collection.domain.vo.RecruitmentImportFailureVO",
            "sheetName"
        );
        assertHasField(
            "com.ruoyi.info.collection.domain.vo.RecruitmentImportFailureVO",
            "sheetRowNum"
        );

        String serviceImpl = Files.readString(
            Path.of("src/main/java/com/ruoyi/info/collection/service/impl/CcdiStaffRecruitmentServiceImpl.java")
        );
        assertTrue(serviceImpl.contains("recruitmentList.size() + workList.size()"));
        assertFalse(serviceImpl.contains("importRecruitmentWork("));
    }

    private void assertHasField(String className, String fieldName) throws Exception {
        Class<?> clazz = Class.forName(className);
        Field field = clazz.getDeclaredField(fieldName);
        assertNotNull(field);
    }
}
