using TraceCad.Core.Geometry;
using TraceCad.Core.Model;
using TraceCad.Dxf;
using Xunit;

namespace TraceCad.Tests;

public sealed class DxfExporterTests
{
    [Fact]
    public void ExportCreatesDxfFile()
    {
        var document = SketchDocument.CreateDefault();
        document.AddEntity(new LineEntity(Guid.NewGuid(), Layer.Cut.Name, new Point2(0, 0), new Point2(100, 0)));
        document.AddEntity(new CircleEntity(Guid.NewGuid(), Layer.Cut.Name, new Point2(20, 20), 10));
        document.AddEntity(new ArcEntity(Guid.NewGuid(), Layer.Cut.Name, new Point2(40, 40), 12, 0, 180, false));

        var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid():N}.dxf");

        try
        {
            new NetDxfExporter().Export(document, path);

            Assert.True(File.Exists(path));
            Assert.True(new FileInfo(path).Length > 0);
            Assert.Contains("SECTION", File.ReadAllText(path));
        }
        finally
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
        }
    }

    [Fact]
    public void ExportRejectsInvalidDrawing()
    {
        var document = SketchDocument.CreateDefault();
        document.AddEntity(new LineEntity(Guid.NewGuid(), Layer.Cut.Name, new Point2(5, 5), new Point2(5, 5)));

        var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid():N}.dxf");

        try
        {
            var exception = Assert.Throws<InvalidOperationException>(() =>
                new NetDxfExporter().Export(document, path));

            Assert.Contains("Cannot export invalid drawing", exception.Message);
            Assert.False(File.Exists(path));
        }
        finally
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
        }
    }
}
