using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace ScadaLink.StoreAndForward;

public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddStoreAndForward(this IServiceCollection services)
    {
        services.AddSingleton<StoreAndForwardStorage>(sp =>
        {
            var options = sp.GetRequiredService<IOptions<StoreAndForwardOptions>>().Value;
            var logger = sp.GetRequiredService<ILogger<StoreAndForwardStorage>>();
            return new StoreAndForwardStorage(
                $"Data Source={options.SqliteDbPath}",
                logger);
        });

        services.AddSingleton<StoreAndForwardService>(sp =>
        {
            var storage = sp.GetRequiredService<StoreAndForwardStorage>();
            var options = sp.GetRequiredService<IOptions<StoreAndForwardOptions>>().Value;
            var logger = sp.GetRequiredService<ILogger<StoreAndForwardService>>();
            var replication = sp.GetRequiredService<ReplicationService>();
            return new StoreAndForwardService(storage, options, logger, replication);
        });

        services.AddSingleton<ReplicationService>(sp =>
        {
            var options = sp.GetRequiredService<IOptions<StoreAndForwardOptions>>().Value;
            var logger = sp.GetRequiredService<ILogger<ReplicationService>>();
            return new ReplicationService(options, logger);
        });

        return services;
    }

    public static IServiceCollection AddStoreAndForwardActors(this IServiceCollection services)
    {
        // Akka actor registration handled by Host component during actor system startup
        return services;
    }
}
