using Kompas.Mcp.Core.Documents;
using Kompas.Mcp.Core.Drawings;
using Kompas.Mcp.Core.Modeling;

namespace Kompas.Mcp.Tests.Integration;

/// <summary>Интеграция: радиальный размер (R) на виде (API7 ISymbols2DContainer.RadialDimensions).</summary>
[Trait("Category", "Integration")]
[Collection(KompasCollection.Name)]
public sealed class DrawingRadialTests : IntegrationTestBase
{
    private readonly DocumentService _docs;
    private readonly PartModeler _modeler;
    private readonly DrawingService _drawing;

    public DrawingRadialTests(KompasFixture fx) : base(fx)
    {
        _docs = new DocumentService(fx.Session, fx.Dispatcher);
        _modeler = new PartModeler(fx.Session, fx.Dispatcher);
        _drawing = new DrawingService(fx.Session, fx.Dispatcher);
    }

    private async Task<string> BuildAndSaveBoxAsync()
    {
        var path = TestPaths.NewFile(".m3d");
        await _docs.CreateAsync(KompasDocumentType.Part);
        try
        {
            var s = await _modeler.OpenSketchAsync(BasePlane.XOY);
            await _modeler.AddRectangleAsync(s, 0, 0, 40, 30);
            await _modeler.CloseSketchAsync(s);
            await _modeler.ExtrudeAsync(s, depth: 20);
            await _modeler.RebuildAsync();
            await _docs.SaveAsAsync(path);
        }
        finally { await _docs.CloseAsync(save: false); }
        return path;
    }

    [Fact]
    public async Task Radial_dimension_value_is_radius()
    {
        var partFile = await BuildAndSaveBoxAsync();
        await _docs.CreateAsync(KompasDocumentType.Drawing);
        try
        {
            await _drawing.CreateStandardViewsAsync(partFile, 1.0, 100, 150);
            // Радиус 12 → значение радиального = радиус (НЕ диаметр), подтверждено спайком.
            var r = await _drawing.AddRadialDimensionAsync(viewNumber: 0, xc: 10, yc: 10, radius: 12, angleDeg: 45);
            Assert.InRange(r.Value, 11.9, 12.1);
        }
        finally { await _docs.CloseAsync(save: false); }
    }

    [Fact]
    public async Task Targets_specific_view_and_increments_radial_count()
    {
        var partFile = await BuildAndSaveBoxAsync();
        await _docs.CreateAsync(KompasDocumentType.Drawing);
        try
        {
            var views = await _drawing.CreateStandardViewsAsync(partFile, 1.0, 100, 150);
            var targetNumber = views.ViewNumbers[1];

            var before = await _drawing.GetViewRadialDimensionCountAsync(targetNumber);
            var r = await _drawing.AddRadialDimensionAsync(targetNumber, xc: 5, yc: 5, radius: 8, angleDeg: 30);
            var after = await _drawing.GetViewRadialDimensionCountAsync(targetNumber);

            Assert.Equal(targetNumber, r.ViewNumber);
            Assert.Equal(before + 1, after);
            Assert.InRange(r.Value, 7.9, 8.1);
        }
        finally { await _docs.CloseAsync(save: false); }
    }

    [Fact]
    public async Task Non_positive_radius_throws()
    {
        var partFile = await BuildAndSaveBoxAsync();
        await _docs.CreateAsync(KompasDocumentType.Drawing);
        try
        {
            await _drawing.CreateStandardViewsAsync(partFile, 1.0, 100, 150);
            await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() =>
                _drawing.AddRadialDimensionAsync(viewNumber: 0, xc: 0, yc: 0, radius: 0, angleDeg: 0));
        }
        finally { await _docs.CloseAsync(save: false); }
    }

    [Fact]
    public async Task No_views_throws()
    {
        await _docs.CreateAsync(KompasDocumentType.Drawing);
        try
        {
            await Assert.ThrowsAsync<InvalidOperationException>(() =>
                _drawing.AddRadialDimensionAsync(viewNumber: 0, xc: 0, yc: 0, radius: 10, angleDeg: 0));
        }
        finally { await _docs.CloseAsync(save: false); }
    }

    [Fact]
    public async Task Unknown_view_number_throws()
    {
        var partFile = await BuildAndSaveBoxAsync();
        await _docs.CreateAsync(KompasDocumentType.Drawing);
        try
        {
            await _drawing.CreateStandardViewsAsync(partFile, 1.0, 100, 150);
            await Assert.ThrowsAsync<InvalidOperationException>(() =>
                _drawing.AddRadialDimensionAsync(viewNumber: 999, xc: 0, yc: 0, radius: 10, angleDeg: 0));
        }
        finally { await _docs.CloseAsync(save: false); }
    }
}
