using Microsoft.Data.SqlClient;

namespace ZB.MOM.WW.MxGateway.Server.Dashboard;

public static class DashboardConnectionStringDisplay
{
    /// <summary>Returns a sanitized Galaxy Repository connection string for display.</summary>
    /// <param name="connectionString">The connection string to sanitize.</param>
    /// <returns>A connection string with only display-safe fields, or a placeholder if it cannot be parsed.</returns>
    public static string GalaxyRepositoryConnectionString(string connectionString)
    {
        try
        {
            SqlConnectionStringBuilder builder = new(connectionString);
            SqlConnectionStringBuilder display = new()
            {
                DataSource = builder.DataSource,
                InitialCatalog = builder.InitialCatalog,
                IntegratedSecurity = builder.IntegratedSecurity,
                Encrypt = builder.Encrypt,
                TrustServerCertificate = builder.TrustServerCertificate,
            };

            return display.ConnectionString;
        }
        catch (ArgumentException)
        {
            return "[invalid connection string]";
        }
    }
}
