using DigitalData.Core.Abstraction.Application;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;

namespace DigitalData.Core.Application
{
    /// <summary>
    /// Provides extension methods to <see cref="IServiceCollection"/> for registering Clean Architecture CRUD related services and repositories.
    /// </summary>
    public static class DIExtensions
    {
        /// <summary>
        /// Adds the directory search service to the <see cref="IServiceCollection"/>.
        /// </summary>
        /// <param name="service">The <see cref="IServiceCollection"/> to add services to.</param>
        /// <param name="directorySearchOptions">
        /// Optional. An instance of <see cref="DirectorySearchOptions"/> to configure the directory search service.
        /// If not provided, the options need to be configured separately.
        /// </param>
        /// <returns>The updated <see cref="IServiceCollection"/>.</returns>
        /// <remarks>
        /// This method adds the necessary services for directory search functionality, including memory caching.
        /// If <paramref name="directorySearchOptions"/> is not provided, ensure to configure the options separately
        /// using the <see cref="IOptions{TOptions}"/> pattern.
        /// </remarks>
        public static IServiceCollection AddDirectorySearchService(this IServiceCollection service, IConfigurationSection directorySearchOptions)
        {
            return service.Configure<DirectorySearchOptions>(directorySearchOptions)
                .AddMemoryCache()
                .AddScoped<IDirectorySearchService, DirectorySearchService>();
        }

        /// <summary>
        /// Adds the JWT service to the <see cref="IServiceCollection"/>.
        /// </summary>
        /// <typeparam name="TClaimValue">The type of the claim value used in the JWT token.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
        /// <param name="tokenDescriptorFactory">A function that takes a claim value of type <typeparamref name="TClaimValue"/> and returns a <see cref="SecurityTokenDescriptor"/> used to configure the JWT token.</param>
        /// <returns>The original <see cref="IServiceCollection"/> instance, allowing further configuration.</returns>
        /// <remarks>
        /// This method adds the necessary services for handling JWT tokens. The <paramref name="tokenDescriptorFactory"/> function is used to generate the <see cref="SecurityTokenDescriptor"/> which is essential for creating the JWT tokens.
        /// </remarks>
        public static IServiceCollection AddJWTService<TClaimValue>(this IServiceCollection services, Func<TClaimValue, SecurityTokenDescriptor> tokenDescriptorFactory)
        {
            return services.AddScoped<IJWTService<TClaimValue>, JWTService<TClaimValue>>(provider => new (tokenDescriptorFactory));
        }
    }
}