namespace NexusInstaller.Services;

/// <summary>
/// Identity SQLite lives outside the versioned install tree (data/identity under POC root or LocalAppData).
/// </summary>
internal static class NexusIdentityDataPaths
{
    public static string ResolveDataDirectory(string installRoot)
    {
        if (!string.IsNullOrWhiteSpace(installRoot))
        {
            return Path.Combine(installRoot, "data", "identity");
        }

        return Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
            "NinjaNexus",
            "identity");
    }

    public static string ResolveDatabasePath(string installRoot)
    {
        var dataDir = ResolveDataDirectory(installRoot);
        Directory.CreateDirectory(dataDir);
        return Path.Combine(dataDir, "nexus-identity.db");
    }

    public static string BuildConnectionString(string installRoot) =>
        $"Data Source={ResolveDatabasePath(installRoot)}";

    public static void EnsureDataDirectories(string installRoot)
    {
        Directory.CreateDirectory(installRoot);
        Directory.CreateDirectory(Path.Combine(installRoot, "versions"));
        Directory.CreateDirectory(Path.Combine(installRoot, "current"));
        Directory.CreateDirectory(Path.Combine(installRoot, "data"));
        Directory.CreateDirectory(ResolveDataDirectory(installRoot));
        Directory.CreateDirectory(Path.Combine(installRoot, "logs"));
        Directory.CreateDirectory(Path.Combine(installRoot, "cache"));
        Directory.CreateDirectory(Path.Combine(installRoot, "cache", "staging"));
    }
}
