package com.ruoyi.ccdi.project.domain.entity;

import org.junit.jupiter.api.Test;

import java.nio.file.Files;
import java.nio.file.Path;

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

class CcdiProjectOverviewEmployeeResultEntityTest {

    private static final Path ENTITY_PATH = Path.of(
        "src/main/java/com/ruoyi/ccdi/project/domain/entity/CcdiProjectOverviewEmployeeResult.java"
    );
    @Test
    void entityAndSqlShouldDefineProjectOverviewEmployeeResultTable() throws Exception {
        String entitySource = Files.readString(ENTITY_PATH);
        String sql = Files.readString(resolveSqlPath());
        String normalizedSql = sql.toLowerCase().replace("`", "");

        assertAll(
            () -> assertTrue(entitySource.contains("@TableName(\"ccdi_project_overview_employee_result\")")),
            () -> assertTrue(entitySource.contains("private Long projectId;")),
            () -> assertTrue(entitySource.contains("private String staffIdCard;")),
            () -> assertTrue(entitySource.contains("private Integer ruleCount;")),
            () -> assertTrue(entitySource.contains("private Integer modelCount;")),
            () -> assertTrue(entitySource.contains("private Integer hitCount;")),
            () -> assertTrue(entitySource.contains("private String riskLevelCode;")),
            () -> assertTrue(entitySource.contains("private String modelCodesCsv;")),
            () -> assertTrue(entitySource.contains("private String modelNamesJson;")),
            () -> assertTrue(entitySource.contains("private String hitRulesJson;")),
            () -> assertTrue(entitySource.contains("private String modelHitSummaryJson;")),
            () -> assertTrue(normalizedSql.contains("create table if not exists ccdi_project_overview_employee_result")),
            () -> assertTrue(normalizedSql.contains("unique key")),
            () -> assertTrue(normalizedSql.contains("project_id")),
            () -> assertTrue(normalizedSql.contains("staff_id_card")),
            () -> assertTrue(normalizedSql.contains("rule_count")),
            () -> assertTrue(normalizedSql.contains("model_count")),
            () -> assertTrue(normalizedSql.contains("hit_count")),
            () -> assertTrue(normalizedSql.contains("risk_level_code")),
            () -> assertTrue(normalizedSql.contains("model_codes_csv")),
            () -> assertTrue(normalizedSql.contains("model_names_json")),
            () -> assertTrue(normalizedSql.contains("hit_rules_json")),
            () -> assertTrue(normalizedSql.contains("model_hit_summary_json"))
        );
    }

    private Path resolveSqlPath() {
        Path moduleRelative = Path.of("../sql/migration/2026-03-20-create-project-overview-employee-result-table.sql");
        if (Files.exists(moduleRelative)) {
            return moduleRelative;
        }
        return Path.of("sql/migration/2026-03-20-create-project-overview-employee-result-table.sql");
    }
}
