using Kompas.Mcp.Core.Documents;
using Kompas.Mcp.Core.Vision;

namespace Kompas.Mcp.Tests.Integration;

/// <summary>Интеграция: снимок модели в растр («зрение агента»).</summary>
[Trait("Category", "Integration")]
[Collection(KompasCollection.Name)]
public sealed class SnapshotTests : IntegrationTestBase
{
    private readonly DocumentService _docs;
    private readonly SnapshotService _snap;

    public SnapshotTests(KompasFixture fx) : base(fx)
    {
        _docs = new DocumentService(fx.Session, fx.Dispatcher);
        _snap = new SnapshotService(fx.Session, fx.Dispatcher);
    }

    [Fact]
    public async Task Snapshot_active_part_returns_valid_png()
    {
        await _docs.CreateAsync(KompasDocumentType.Part);
        try
        {
            var path = TestPaths.NewFile(".png");
            var snap = await _snap.CaptureActiveModelAsync(RasterImageFormat.Png, saveToFile: path);

            Assert.Equal(RasterImageFormat.Png, snap.Format);
            Assert.True(snap.Bytes.Length > 100, "Снимок подозрительно мал.");

            // Сигнатура PNG: 89 50 4E 47 0D 0A 1A 0A
            Assert.Equal(0x89, snap.Bytes[0]);
            Assert.Equal((byte)'P', snap.Bytes[1]);
            Assert.Equal((byte)'N', snap.Bytes[2]);
            Assert.Equal((byte)'G', snap.Bytes[3]);

            Assert.True(File.Exists(path));
        }
        finally
        {
            await _docs.CloseAsync(save: false);
        }
    }
}
