using Configuration;
using Microsoft.Extensions.Options;
using Services;

public static class ServiceCollectionExtension
{
    public static WebApplicationBuilder AddCustomComponents(this WebApplicationBuilder services)
    {
        services.Services.AddOptions<SqlConnectionOptions>().BindConfiguration("SqlConnectionFactory");

        services.Services.AddSingleton<SqlConnectionFactory>(sp =>
        {
            var options = sp.GetService<IOptions<SqlConnectionOptions>>()!.Value;

            return new SqlConnectionFactory(options.ConnectionUrl);
        });

        services.Services.AddScoped<IRentService, RentService>();
        services.Services.AddScoped<IRollbackService, RollbackService>();
        services.Services.AddScoped<IErrorService, ErrorService>();
        return services;
    }
}