using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Configuration;

namespace DigitalData.Core.DTO
{
    /// <summary>
    /// Provides extension methods for dependency injection.
    /// </summary>
    public static class DIExtensions
    {
        /// <summary>
        /// Adds the <see cref="CookieConsentSettings"/> to the service collection.
        /// </summary>
        /// <param name="services">The service collection to add the settings to.</param>
        /// <returns>The updated service collection.</returns>
        /// <exception cref="ConfigurationErrorsException">
        /// Thrown if the 'CookieConsentSettings' section is missing or improperly configured in appsettings.json.
        /// </exception>
        public static IServiceCollection AddCookieConsentSettings(this IServiceCollection services)
        {
            services.AddSingleton(sp =>
            {
                var configuration = sp.GetRequiredService<IConfiguration>();
                var settings = configuration.GetSection("CookieConsentSettings").Get<CookieConsentSettings>();
                return settings is null
                    ? throw new ConfigurationErrorsException("The 'CookieConsentSettings' section is missing or improperly configured in appsettings.json.")
                    : settings;
            });
            return services;
        }
    }
}