package cn.yunzhixue.ability.center.examsprint.infrastructure.report.rendering.outlook;

import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import static org.assertj.core.api.Assertions.assertThat;

class OutlookExamSprintReportTemplateCompatibilityTest {

    @Test
    void templateMatchesDesignDraftWithPdfSafeLayoutStyles() throws Exception {
        String normalizedTemplate = normalizeWhitespace(loadTemplate());

        assertThat(normalizedTemplate)
                .contains("body")
                .contains("background-color: #f5f7fa")
                .contains("padding: 0")
                .contains("color: #333")
                .contains("font-family: MiSans, ReportFont, sans-serif")
                .contains("font-size: 14px")
                .contains("line-height: 1.72")
                .doesNotContainPattern("(?i)-webkit-print-color-adjust\\s*:")
                .doesNotContainPattern("(?i)(^|[\\s{;])print-color-adjust\\s*:")
                .doesNotContain("Microsoft YaHei")
                .contains(".report-container")
                .contains("max-width: 1200px")
                .contains("margin: 0 auto")
                .contains("background: #fff")
                .contains("padding: 32px")
                .contains("border-radius: 12px")
                .contains("border: 1px solid #e7edf5")
                .doesNotContainPattern("(?i)(^|[\\s{;])box-shadow\\s*:")
                .contains("h1.report-title")
                .contains("font-size: 28px")
                .containsPattern("h1\\.report-title\\s*\\{[^}]*font-family\\s*:\\s*MiSans, ReportFont, sans-serif\\s*;[^}]*font-size\\s*:\\s*28px\\s*;[^}]*font-weight\\s*:\\s*600\\s*;[^}]*}")
                .contains("p.report-subtitle")
                .containsPattern("p\\.report-subtitle\\s*\\{[^}]*font-family\\s*:\\s*MiSans, ReportFont, sans-serif\\s*;[^}]*line-height\\s*:\\s*1.72\\s*;[^}]*}")
                .contains(".section")
                .contains(".section-title")
                .containsPattern("\\.section-title\\s*\\{[^}]*font-family\\s*:\\s*MiSans, ReportFont, sans-serif\\s*;[^}]*font-size\\s*:\\s*20px\\s*;[^}]*font-weight\\s*:\\s*600\\s*;[^}]*}")
                .contains("border-left: 6px solid #ff7d00")
                .contains(".analysis-table")
                .contains(".analysis-row")
                .contains(".analysis-cell")
                .containsPattern("\\.analysis-table\\s*\\{[^}]*width\\s*:\\s*100%\\s*;[^}]*table-layout\\s*:\\s*fixed\\s*;[^}]*}")
                .containsPattern("\\.analysis-row\\s*\\{[^}]*page-break-inside\\s*:\\s*avoid\\s*;[^}]*}")
                .containsPattern("\\.card\\s*\\{[^}]*page-break-inside\\s*:\\s*avoid\\s*;[^}]*}")
                .containsPattern("\\.card\\s*\\{[^}]*min-height\\s*:\\s*370px\\s*;[^}]*}")
                .doesNotContainPattern("(?i)(^|[\\s{;])display\\s*:\\s*flex\\b")
                .doesNotContainPattern("(?i)(^|[\\s{;])flex-direction\\s*:")
                .containsPattern("\\.card-title\\s*\\{[^}]*font-family\\s*:\\s*MiSans, ReportFont, sans-serif\\s*;[^}]*font-size\\s*:\\s*16px\\s*;[^}]*font-weight\\s*:\\s*600\\s*;[^}]*}")
                .contains(".frequency-table")
                .contains(".frequency-row")
                .contains(".frequency-cell")
                .containsPattern("\\.frequency-table\\s*\\{[^}]*width\\s*:\\s*100%\\s*;[^}]*table-layout\\s*:\\s*fixed\\s*;[^}]*}")
                .containsPattern("\\.frequency-row\\s*\\{[^}]*page-break-inside\\s*:\\s*avoid\\s*;[^}]*}")
                .containsPattern("\\.freq-card\\s*\\{[^}]*page-break-inside\\s*:\\s*avoid\\s*;[^}]*}")
                .contains(".freq-header")
                .containsPattern("\\.freq-header\\s*\\{[^}]*font-family\\s*:\\s*MiSans, ReportFont, sans-serif\\s*;[^}]*font-size\\s*:\\s*18px\\s*;[^}]*font-weight\\s*:\\s*600\\s*;[^}]*}")
                .containsPattern("\\.badge\\s*\\{[^}]*position\\s*:\\s*relative\\s*;[^}]*top\\s*:\\s*7px\\s*;[^}]*min-width\\s*:\\s*40px\\s*;[^}]*height\\s*:\\s*20px\\s*;[^}]*padding\\s*:\\s*0\\s+8px\\s*;[^}]*line-height\\s*:\\s*20px\\s*;[^}]*text-align\\s*:\\s*center\\s*;[^}]*vertical-align\\s*:\\s*middle\\s*;[^}]*}")
                .contains(".freq-header-table")
                .containsPattern("\\.freq-header-table\\s*\\{[^}]*width\\s*:\\s*100%\\s*;[^}]*table-layout\\s*:\\s*fixed\\s*;[^}]*border-collapse\\s*:\\s*collapse\\s*;[^}]*}")
                .containsPattern("\\.freq-title-cell\\s*\\{[^}]*vertical-align\\s*:\\s*middle\\s*;[^}]*padding\\s*:\\s*0\\s*;[^}]*}")
                .containsPattern("\\.freq-star-cell\\s*\\{[^}]*width\\s*:\\s*28px\\s*;[^}]*text-align\\s*:\\s*right\\s*;[^}]*vertical-align\\s*:\\s*middle\\s*;[^}]*}")
                .doesNotContainPattern("\\.crown\\s*\\{[^}]*position\\s*:\\s*absolute\\s*;")
                .containsPattern("\\.crown\\s*\\{[^}]*position\\s*:\\s*relative\\s*;[^}]*top\\s*:\\s*-4px\\s*;[^}]*}")
                .containsPattern("\\.data-text\\s*\\{[^}]*font-family\\s*:\\s*MiSans, ReportFont, sans-serif\\s*;[^}]*line-height\\s*:\\s*1.72\\s*;[^}]*}")
                .contains(".text-desc")
                .containsPattern("\\.text-desc\\s*\\{[^}]*font-family\\s*:\\s*MiSans, ReportFont, sans-serif\\s*;[^}]*line-height\\s*:\\s*1.72\\s*;[^}]*}")
                .containsPattern("\\.freq-data\\s*\\{[^}]*font-family\\s*:\\s*MiSans, ReportFont, sans-serif\\s*;[^}]*line-height\\s*:\\s*1.72\\s*;[^}]*}")
                .contains(".student-case")
                .contains(".student-case-table")
                .contains(".student-case-row")
                .contains(".case-chart-cell")
                .contains(".case-info-cell")
                .containsPattern("\\.student-case\\s*\\{[^}]*page-break-inside\\s*:\\s*avoid\\s*;[^}]*}")
                .containsPattern("<div\\s+class=\"(?=[^\"]*\\bsection\\b)(?=[^\"]*\\bmodule-section-two\\b)[^\"]*\">\\s*<h2 class=\"section-title\">模块二：科学备考建议</h2>")
                .containsPattern("@media print\\s*\\{[^}]*\\.module-section-two\\s*\\{[^}]*margin-top\\s*:\\s*32px\\s*;[^}]*}[^}]*\\.page-break-before-module2\\s*\\{[^}]*page-break-before\\s*:\\s*always\\s*;[^}]*height\\s*:\\s*0\\s*;[^}]*margin\\s*:\\s*0\\s*;[^}]*padding\\s*:\\s*0\\s*;[^}]*line-height\\s*:\\s*0\\s*;[^}]*font-size\\s*:\\s*0\\s*;[^}]*}")
                .doesNotContainPattern("(?i):nth-of-type\\s*\\(")
                .doesNotContainPattern("(?i)(^|[\\s{;])break-before\\s*:")
                .doesNotContainPattern("\\.analysis-grid\\s*\\{[^}]*display\\s*:\\s*grid\\s*;")
                .doesNotContainPattern("\\.frequency-grid\\s*\\{[^}]*display\\s*:\\s*flex\\s*;")
                .doesNotContainPattern("\\.student-case\\s*\\{[^}]*display\\s*:\\s*flex\\s*;")
                .doesNotContainPattern("\\.section\\s*\\{[^}]*page-break-inside\\s*:\\s*avoid\\s*;");
    }

    @Test
    void templateUsesH2ForAllModuleSectionTitles() throws Exception {
        String normalizedTemplate = normalizeWhitespace(loadTemplate());

        assertThat(normalizedTemplate)
                .contains("<h2 class=\"section-title\">模块一：个人学情分析</h2>")
                .contains("<h2 class=\"section-title\">模块二：科学备考建议</h2>")
                .contains("<h2 class=\"section-title\">模块三：上届学员提分案例</h2>")
                .doesNotContain("<div class=\"section-title\">");
    }

    @Test
    void templateContainsRequiredRendererSlotsForDynamicSections() throws Exception {
        String normalizedTemplate = normalizeWhitespace(loadTemplate());

        assertThat(normalizedTemplate)
                .contains("{{syllabusMasterySection}}")
                .contains("{{pastPaperVocabularySection}}")
                .contains("{{highFrequencyVocabularySection}}")
                .contains("{{frequencyBandSection}}")
                .contains("{{caseStudySection}}")
                .contains("{{studySuggestionSection}}")
                .contains("<table class=\"analysis-table\" role=\"presentation\">")
                .contains("<tr class=\"analysis-row\">")
                .contains("<td class=\"analysis-cell analysis-cell-left\">{{syllabusMasterySection}}</td>")
                .contains("<td class=\"analysis-cell analysis-cell-right\">{{pastPaperVocabularySection}}</td>")
                .contains("<td class=\"analysis-cell analysis-cell-left\">{{highFrequencyVocabularySection}}</td>")
                .contains("<td class=\"analysis-cell analysis-cell-right\">{{frequencyBandSection}}</td>")
                .doesNotContain("{{reportIntroShell}}");
    }

    @Test
    void templateKeepsA4PageSizeAndReportTitleHierarchyFromDesignDraft() throws Exception {
        String normalizedTemplate = normalizeWhitespace(loadTemplate());

        assertThat(normalizedTemplate)
                .containsPattern("@page\\s*\\{[^}]*size\\s*:\\s*A4\\s*;[^}]*margin\\s*:\\s*0\\s*;[^}]*}")
                .contains("h1.report-title")
                .contains("font-family: MiSans, ReportFont, sans-serif")
                .contains("font-size: 28px")
                .contains("font-weight: 600")
                .contains("color: #2b4c8a")
                .contains("p.report-subtitle")
                .contains("line-height: 1.72")
                .contains("text-align: center");
    }

    private String loadTemplate() throws Exception {
        ClassPathResource resource = new ClassPathResource("templates/outlook-exam-sprint-report-template.html");
        try (InputStream inputStream = resource.getInputStream()) {
            return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
        }
    }

    private String normalizeWhitespace(String value) {
        return value.replaceAll("\\s+", " ").trim();
    }
}
