using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Common.Settings;

namespace StartupHelpers;

public static class DatabaseExtensions
{
    public static string GetConfiguredDbConnectionString(this IServiceCollection services, IConfiguration configuration)
    {
        var dbOptions = configuration
    .GetSection("Database")
    .Get<DatabaseSettings>()
    ?? throw new InvalidOperationException("Database config missing");

        return
            $"Server={dbOptions.Host},{dbOptions.Port};" +
            $"Database={dbOptions.Name};" +
            $"User Id={dbOptions.User};" +
            $"Password={dbOptions.Password};" +
            $"TrustServerCertificate={dbOptions.TrustServerCertificate};";
    }
}
