using DocumentFormat.OpenXml.Packaging;
using Jiaowu.Api.Controllers;
using Jiaowu.Api.Infrastructure.Grades;

namespace Jiaowu.Api.Tests;

public sealed class GradeAnalysisWordReportGeneratorTests
{
    [Fact]
    public void Generate_CreatesReadableDocxWithAnalysisSectionsAndCharts()
    {
        var now = new DateTime(2026, 8, 9, 10, 30, 0, DateTimeKind.Local);
        var summary = new GradeAnalyticsController.TeachingClassMetrics(
            10, 9, 2, 98m, 78.5m, 80m, 52m, 12.34m, 90m, 20m, now,
            [
                new("0–59", 0, 60, 1),
                new("60–69", 60, 70, 2),
                new("70–79", 70, 80, 2),
                new("80–89", 80, 90, 3),
                new("90–100", 90, null, 2)
            ]);
        var report = new GradeAnalyticsController.TeachingClassAnalysisReport(
            false, Guid.NewGuid(), Guid.NewGuid(), "TASK-01", "计算机一班",
            "CS101", "程序设计", "2026-2027 学年第一学期", summary,
            [new(Guid.NewGuid(), Guid.NewGuid(), "TASK-01", "计算机一班", "张老师", "计科一班", 10, 98m, 78.5m, 80m, 52m, 12.34m, 90m, 20m, true)],
            [new("全校", "全校同课程", 100, 100m, 76m, 45m, 88m)],
            [new(Guid.NewGuid(), "2026-2027 学年第一学期", 100, 76m, 88m, 18m, new(10, 78.5m, 90m, 20m))],
            new(2.5m, 2m, 76m, 88m));

        var bytes = GradeAnalysisWordReportGenerator.Generate(report, now);

        Assert.True(bytes.Length > 10_000);
        using var stream = new MemoryStream(bytes);
        using var document = WordprocessingDocument.Open(stream, false);
        var text = document.MainDocumentPart!.Document.InnerText;
        Assert.Contains("成绩分析报告", text);
        Assert.Contains("同课程教学班对比", text);
        Assert.Contains("各范围基准", text);
        Assert.Contains("历年成绩趋势", text);
        Assert.Equal(3, document.MainDocumentPart.ImageParts.Count());
        Assert.Equal(2, document.MainDocumentPart.HeaderParts.Count());
        Assert.Equal(2, document.MainDocumentPart.FooterParts.Count());
    }
}
