using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using NPin.Framework.SqlSugarCore.Abstractions;

namespace NPin.Framework.SqlSugarCore;

public static class SqlSugarCoreExtensions
{
    public static IServiceCollection AddNPinDbContext<TDbContext>(this IServiceCollection service)
        where TDbContext : class, ISqlSugarDbContext
    {
        service.Replace(new ServiceDescriptor(typeof(ISqlSugarDbContext), typeof(TDbContext),
            ServiceLifetime.Transient));
        return service;
    }

    public static IServiceCollection TryAddNPinDbContext<TDbContext>(this IServiceCollection service)
        where TDbContext : class, ISqlSugarDbContext
    {
        service.TryAdd(new ServiceDescriptor(typeof(ISqlSugarDbContext), typeof(TDbContext),
            ServiceLifetime.Transient));
        return service;
    }

    public static IServiceCollection AddNPinDbContext<TDbContext>(this IServiceCollection service,
        Action<DbConnOptions> options)
        where TDbContext : class, ISqlSugarDbContext
    {
        service.Configure<DbConnOptions>(options.Invoke);
        service.AddNPinDbContext<TDbContext>();
        return service;
    }
}