using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MiningService.Domain.AggregatesModel.MinerAggregate;
using MiningService.Domain.AggregatesModel.CircleAggregate;
using MiningService.Domain.AggregatesModel.ReferralAggregate;
using MiningService.Domain.AggregatesModel.ConfigurationAggregate;
using MiningService.Infrastructure.Repositories;

namespace MiningService.Infrastructure;

/// <summary>
/// EN: Extension methods for infrastructure service registration.
/// VI: Extension methods để đăng ký infrastructure services.
/// </summary>
public static class DependencyInjection
{
    public static IServiceCollection AddInfrastructure(
        this IServiceCollection services,
        IConfiguration configuration)
    {
        // Database
        var connectionString = configuration.GetConnectionString("DefaultConnection")
            ?? configuration["DATABASE_URL"]
            ?? throw new InvalidOperationException("Database connection string not found");

        services.AddDbContext<MiningServiceContext>(options =>
        {
            options.UseNpgsql(connectionString, npgsqlOptions =>
            {
                npgsqlOptions.EnableRetryOnFailure(
                    maxRetryCount: 5,
                    maxRetryDelay: TimeSpan.FromSeconds(30),
                    errorCodesToAdd: null);
            });
        });

        // Repositories
        services.AddScoped<IMinerRepository, MinerRepository>();
        services.AddScoped<ICircleRepository, CircleRepository>();
        services.AddScoped<IReferralRepository, ReferralRepository>();
        services.AddScoped<IConfigurationRepository, ConfigurationRepository>();

        return services;
    }
}
