namespace QuantEngine.Core.Tests;

public class UnitTest1
{
    [Fact]
    public void OperationalReportLoader_ParsesCanonicalTempReport()
    {
        var path = Path.Combine(AppContext.BaseDirectory, "Fixtures", "operational_report.json");
        var report = QuantEngine.Core.Infrastructure.OperationalReportLoader.Load(path);

        Assert.Equal("2026-05-24-operational-report-v1", report.SchemaVersion);
        Assert.Equal("GatherTradingData.json", report.SourceJson);
        Assert.Equal(38, report.SectionCount);
        Assert.True(report.Sections.Count >= 4);
        Assert.Equal("exec_safety_declaration", report.Sections[0].Name);
        Assert.Contains("source: .NET operational report builder", report.Sections[0].Preview);
    }

    [Fact]
    public void OperationalReportLoader_ReturnsSafeDefaultsWhenFileIsMissing()
    {
        var path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"), "operational_report.json");
        var report = QuantEngine.Core.Infrastructure.OperationalReportLoader.Load(path);

        Assert.Equal("n/a", report.SchemaVersion);
        Assert.Equal("n/a", report.SourceJson);
        Assert.Equal("n/a", report.GeneratedAt);
        Assert.Equal(0, report.SectionCount);
        Assert.Empty(report.Sections);
    }

    [Fact]
    public void OperationalReportLoader_UsesSectionCountFromPayloadWhenPresent()
    {
        var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
        Directory.CreateDirectory(tempDir);
        var path = Path.Combine(tempDir, "operational_report.json");

        File.WriteAllText(path, """
        {
          "schema_version": "test-schema",
          "source_json": "fixture.json",
          "generated_at": "2026-06-26T00:00:00+00:00",
          "section_count": 2,
          "sections": [
            { "name": "alpha", "title": "Alpha", "markdown": "alpha body" },
            { "name": "beta", "title": "Beta", "markdown": "beta body" }
          ]
        }
        """);

        var report = QuantEngine.Core.Infrastructure.OperationalReportLoader.Load(path);

        Assert.Equal("test-schema", report.SchemaVersion);
        Assert.Equal("fixture.json", report.SourceJson);
        Assert.Equal(2, report.SectionCount);
        Assert.Equal(2, report.Sections.Count);
        Assert.Equal("alpha", report.Sections[0].Name);
        Assert.Equal("alpha body", report.Sections[0].Preview);
    }

    [Fact]
    public void OperationalReportLoader_PreservesEmptySectionsWithoutThrowing()
    {
        var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
        Directory.CreateDirectory(tempDir);
        var path = Path.Combine(tempDir, "operational_report.json");

        File.WriteAllText(path, """
        {
          "schema_version": "empty-schema",
          "source_json": "fixture.json",
          "generated_at": "2026-06-26T00:00:00+00:00",
          "section_count": 0,
          "sections": []
        }
        """);

        var report = QuantEngine.Core.Infrastructure.OperationalReportLoader.Load(path);

        Assert.Equal(0, report.SectionCount);
        Assert.Empty(report.Sections);
        Assert.Equal("empty-schema", report.SchemaVersion);
    }
}
