package com.ruoyi.ccdi.project.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;

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

class CcdiProjectSpecialCheckExtendedRecruitmentSqlTest {

    @Test
    void shouldExposeExtendedRecruitmentMapperMethodsAndSqlCaliber() throws Exception {
        Class<?> mapperClass = Class.forName("com.ruoyi.ccdi.project.mapper.CcdiProjectSpecialCheckMapper");
        Class<?> queryClass = Class.forName("com.ruoyi.ccdi.project.domain.dto.CcdiProjectExtendedRecruitmentQueryDTO");

        Method listMethod = mapperClass.getMethod("selectExtendedRecruitmentPage", Page.class, queryClass);
        Method detailMethod = mapperClass.getMethod("selectExtendedRecruitmentDetail", Long.class, String.class);
        assertEquals("Page", listMethod.getReturnType().getSimpleName());
        assertEquals("CcdiProjectExtendedRecruitmentDetailVO", detailMethod.getReturnType().getSimpleName());

        String xml = Files.readString(Path.of("src/main/resources/mapper/ccdi/project/CcdiProjectSpecialCheckMapper.xml"));
        String listSql = extractSelect(xml, "selectExtendedRecruitmentPage");
        String detailSql = extractSelect(xml, "selectExtendedRecruitmentDetail");

        assertTrue(listSql.contains("projectEmployeeScopeSql"));
        assertTrue(listSql.contains("name=\"projectId\" value=\"query.projectId\""));
        assertTrue(listSql.contains("select distinct r.recruit_id"));
        assertTrue(listSql.contains("interviewer_name1"));
        assertTrue(listSql.contains("interviewer_name2"));
        assertTrue(listSql.contains("concat_ws(' / '"));
        assertTrue(listSql.contains("admit_status"));
        assertTrue(detailSql.contains("projectEmployeeScopeSql"));
        assertTrue(detailSql.contains("recruit_id = #{recruitId}"));
        assertTrue(detailSql.contains("interviewer_name1"));
        assertTrue(detailSql.contains("interviewer_name2"));
    }

    private String extractSelect(String xml, String selectId) {
        String start = "<select id=\"" + selectId + "\"";
        int startIndex = xml.indexOf(start);
        int endIndex = xml.indexOf("</select>", startIndex);
        return xml.substring(startIndex, endIndex);
    }
}
