namespace ClaudeDo.Data;

public static class Paths
{
    /// <summary>
    /// Expands a leading "~" or "%USERPROFILE%" and returns an absolute path.
    /// Relative paths are resolved against <paramref name="baseDir"/> (default: current directory).
    /// </summary>
    public static string Expand(string path, string? baseDir = null)
    {
        if (string.IsNullOrWhiteSpace(path))
            throw new ArgumentException("Path must not be empty.", nameof(path));

        var expanded = Environment.ExpandEnvironmentVariables(path);

        if (expanded.StartsWith("~", StringComparison.Ordinal))
        {
            var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            expanded = home + expanded[1..];
        }

        if (!Path.IsPathRooted(expanded))
            expanded = Path.GetFullPath(expanded, baseDir ?? Environment.CurrentDirectory);

        return Path.GetFullPath(expanded);
    }

    /// <summary>~/.todo-app — parent directory for db, logs, config, sandbox, worktrees.</summary>
    public static string AppDataRoot() =>
        Expand("~/.todo-app");
}
