using Knot.Contracts.Relations.Application.Abstractions;
using Knot.Modules.Relations.Application.Abstractions;
using Knot.Modules.Relations.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Knot.Modules.Relations;

public static class DependencyInjection
{
    public static IServiceCollection AddRelationsModule(this IServiceCollection services, IConfiguration configuration)
    {
        var connectionString = configuration.GetConnectionString("DefaultConnection");
        services.AddDbContext<RelationsDbContext>(options =>
            options.UseNpgsql(connectionString));

        services.AddScoped<IContactsDbContext>(sp => sp.GetRequiredService<RelationsDbContext>());
        services.AddScoped<Knot.Contracts.Relations.Application.Abstractions.IFriendshipRepository, FriendshipRepository>();

        services.AddMediatR(config =>
            config.RegisterServicesFromAssembly(typeof(DependencyInjection).Assembly));

        return services;
    }
}
