using Sdt.Config;
using Sdt.Core;
using Xunit;

namespace DevTool.Tests;

public sealed class ConfigDoctorAutoFixServiceTests
{
    [Fact]
    public void FindMissingWorkingDirectories_ReturnsMissingPaths()
    {
        var root = Path.Combine(Path.GetTempPath(), "sdt-autofix-" + Guid.NewGuid().ToString("N"));
        Directory.CreateDirectory(root);
        Directory.CreateDirectory(Path.Combine(root, "exists"));

        var cfg = new DevToolConfig
        {
            Workflows =
            [
                new WorkflowDefinition
                {
                    Id = "build",
                    Label = "Build",
                    Steps =
                    [
                        new WorkflowStep { Id = "s1", Label = "S1", Action = "dotnet-build", WorkingDir = "exists" },
                        new WorkflowStep { Id = "s2", Label = "S2", Action = "dotnet-build", WorkingDir = "missing/sub" }
                    ]
                }
            ]
        };

        var service = new ConfigDoctorAutoFixService();
        var missing = service.FindMissingWorkingDirectories(cfg, root);

        Assert.Single(missing);
        Assert.EndsWith(Path.Combine("missing", "sub"), missing[0], StringComparison.OrdinalIgnoreCase);
    }

    [Fact]
    public void CreateMissingWorkingDirectories_CreatesPaths()
    {
        var root = Path.Combine(Path.GetTempPath(), "sdt-autofix-" + Guid.NewGuid().ToString("N"));
        var path = Path.Combine(root, "a", "b", "c");
        Directory.CreateDirectory(root);

        var service = new ConfigDoctorAutoFixService();
        var result = service.CreateMissingWorkingDirectories([path]);

        Assert.True(result.Success);
        Assert.True(Directory.Exists(path));
        Assert.Equal(1, result.CreatedDirectories);
    }
}
