package cn.yunzhixue.ability.center.examsprint.application.report;

import cn.yunzhixue.ability.center.examsprint.contracts.report.AchievementExamSprintReportPayload;
import cn.yunzhixue.ability.center.examsprint.domain.report.AchievementReportContent;
import com.fasterxml.jackson.databind.JsonNode;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

final class AchievementReportContentMapper {

    private AchievementReportContentMapper() {
    }

    static AchievementReportContent toDomainContent(AchievementExamSprintReportPayload payload) {
        Objects.requireNonNull(payload, "payload");
        return new AchievementReportContent(
                payload.studentName(),
                reportTitle(payload.stageName()),
                payload.testPaperTitle() + " · 临考突击 · 真实提分效果",
                "恭喜完成临考突击专项训练",
                "基于" + payload.testPaperTitle() + " · 真实学习效果分析",
                new AchievementReportContent.SummaryMetrics(
                        signed(payload.studentImproveWordCount()),
                        signed(payload.testPaperImprovedWordCount()),
                        format(payload.paperMasteryHitRate()) + "%",
                        format(payload.improveStudyEfficiency())),
                new AchievementReportContent.Comparison(
                        payload.studentVocabularyBefore().doubleValue(),
                        payload.studentVocabulary().doubleValue(),
                        format(payload.studentVocabularyBefore()),
                        format(payload.studentVocabulary()),
                        signed(payload.studentImproveWordCount())),
                new AchievementReportContent.Comparison(
                        payload.testPaperBeforMastery().doubleValue(),
                        payload.testPaperLatestMastery().doubleValue(),
                        format(payload.testPaperBeforMastery()),
                        format(payload.testPaperLatestMastery()),
                        signed(payload.testPaperImprovedWordCount())),
                new AchievementReportContent.StageVocabularySummary(
                        payload.stageName(),
                        format(payload.stageVocabulary()),
                        format(payload.studentInitialVocabMastery()),
                        format(payload.studentCurrentVocabMastery()),
                        signed(payload.studentVocabMasteryImprovement())),
                new AchievementReportContent.TestPaperVocabularySummary(
                        payload.testPaperTitle(),
                        format(payload.testPaperWordCount()),
                        format(payload.testPaperBeforUnMastery()),
                        format(payload.testPaperAfterUnMastery()),
                        format(payload.testPaperBeforMasteryRate()),
                        format(payload.testPaperLatestMasteryRate()),
                        signed(payload.testPaperImproveRate())),
                new AchievementReportContent.ExamUnknownWordsHitStatus(
                        format(payload.paperMasteryHitRate()) + "%",
                        format(payload.improveStudyEfficiency()),
                        format(payload.testPaperBeforUnMastery()),
                        format(payload.testPaperAfterUnMastery()),
                        format(payload.testPaperImprovedWordCount()),
                        hitWords(payload.testPaperImprovedWords())));
    }

    private static String reportTitle(String stageName) {
        String normalizedStageName = stageName == null ? "" : stageName.trim();
        if (normalizedStageName.contains("英语")) {
            return normalizedStageName + "临考突击学习成果报告";
        }
        return normalizedStageName + "英语临考突击学习成果报告";
    }

    private static String signed(BigDecimal value) {
        String formatted = format(value);
        return value.signum() > 0 ? "+" + formatted : formatted;
    }

    private static String format(BigDecimal value) {
        return value.stripTrailingZeros().toPlainString();
    }

    private static List<String> hitWords(List<JsonNode> improvedWords) {
        return improvedWords.stream()
                .map(AchievementReportContentMapper::hitWord)
                .filter(word -> !word.isBlank())
                .toList();
    }

    private static String hitWord(JsonNode improvedWord) {
        if (improvedWord.isTextual()) {
            return improvedWord.asText().trim();
        }
        return improvedWord.path("WordSpell").asText("").trim();
    }
}
