namespace CashLineERP.Application.Internal;

internal static class RepositoryRootLocator
{
    public static string Resolve(string? startPath = null)
    {
        var current = new DirectoryInfo(
            string.IsNullOrWhiteSpace(startPath)
                ? AppContext.BaseDirectory
                : Path.GetFullPath(startPath));

        while (current is not null)
        {
            var docsPath = Path.Combine(current.FullName, "docs");
            var srcPath = Path.Combine(current.FullName, "src");
            var opsPath = Path.Combine(current.FullName, "ops");
            if (Directory.Exists(docsPath) && Directory.Exists(srcPath) && Directory.Exists(opsPath))
            {
                return current.FullName;
            }

            current = current.Parent;
        }

        return Path.GetFullPath(startPath ?? AppContext.BaseDirectory);
    }
}
