using Sdt.Config;
using Xunit;

namespace DevTool.Tests;

public sealed class WorkspaceLoaderTests
{
    [Fact]
    public void FindAndLoad_DoesNotThrow_WhenAutoDiscoverHitsStrictLegacyProject()
    {
        var root = Path.Combine(Path.GetTempPath(), "sdt-workspace-" + Guid.NewGuid().ToString("N"));
        Directory.CreateDirectory(root);
        File.WriteAllText(Path.Combine(root, "devtool.json"), """
{
  "name": "legacy",
  "version": "0.1.0",
  "targets": [
    {
      "id": "build",
      "label": "Build",
      "group": "Build",
      "command": "dotnet",
      "args": ["build"],
      "workingDir": ".",
      "dependsOn": []
    }
  ],
  "workflows": []
}
""");

        Environment.SetEnvironmentVariable("SDT_LEGACY_MODE", null);
        var result = WorkspaceLoader.FindAndLoad(root);
        Assert.Null(result);
    }

    [Fact]
    public void ResolveProjectRoot_AcceptsAbsolutePaths()
    {
        var root = Path.GetFullPath(Path.Combine(Path.GetTempPath(), "ws-" + Guid.NewGuid().ToString("N")));
        var abs = Path.GetFullPath(Path.Combine(Path.GetTempPath(), "proj-" + Guid.NewGuid().ToString("N")));
        var project = new WorkspaceProject { Path = abs };

        var resolved = WorkspaceLoader.ResolveProjectRoot(root, project);
        Assert.Equal(abs, resolved, ignoreCase: OperatingSystem.IsWindows());
    }

    [Fact]
    public void WorkspaceProject_AdditionalFields_DefaultsAreSafe()
    {
        var project = new WorkspaceProject();
        Assert.Empty(project.Tags);
        Assert.Empty(project.ToolFamilies);
        Assert.False(project.Disabled);
    }
}
