using Microsoft.Extensions.DependencyInjection;
using Knot.Modules.Klipy.Application.Abstractions;
using Knot.Modules.Klipy.Infrastructure.External;

// Псевдонимы для устранения неоднозначности
using ContractIKlipyClient = Knot.Contracts.Klipy.Application.Abstractions.IKlipyClient;
using ModuleIKlipyClient = Knot.Modules.Klipy.Application.Abstractions.IKlipyClient;

namespace Knot.Modules.Klipy;

public static class DependencyInjection
{
    public static IServiceCollection AddKlipyModule(this IServiceCollection services)
    {
        services.AddHttpClient<ModuleIKlipyClient, KlipyClient>();
        // Также регистрируем contract-интерфейс для Admin модуля
        services.AddScoped<ContractIKlipyClient>(sp =>
            new ContractKlipyClientAdapter(sp.GetRequiredService<ModuleIKlipyClient>()));
        return services;
    }
}

// Адаптер для преобразования между интерфейсами
internal sealed class ContractKlipyClientAdapter : ContractIKlipyClient
{
    private readonly ModuleIKlipyClient _inner;

    public ContractKlipyClientAdapter(ModuleIKlipyClient inner)
    {
        _inner = inner;
    }

    public async Task<bool> TestConnectionAsync(string apiKey, string appName, CancellationToken cancellationToken = default)
    {
        return await _inner.TestConnectionAsync(apiKey, appName, cancellationToken);
    }
}
