using ScadaLink.ClusterInfrastructure;
using ScadaLink.Communication;
using ScadaLink.DataConnectionLayer;
using ScadaLink.ExternalSystemGateway;
using ScadaLink.HealthMonitoring;
using ScadaLink.Host.Actors;
using ScadaLink.Host.Health;
using ScadaLink.NotificationService;
using ScadaLink.SiteEventLogging;
using ScadaLink.SiteRuntime;
using ScadaLink.StoreAndForward;

namespace ScadaLink.Host;

/// <summary>
/// Extracted site-role DI registrations so both Program.cs and tests
/// use the same composition root.
/// </summary>
public static class SiteServiceRegistration
{
    public static void Configure(IServiceCollection services, IConfiguration config)
    {
        // Shared components
        services.AddClusterInfrastructure();
        services.AddCommunication();
        services.AddSiteHealthMonitoring();
        services.AddExternalSystemGateway();
        services.AddNotificationService();

        // Health report transport: sends SiteHealthReport to SiteCommunicationActor via Akka
        services.AddSingleton<ISiteIdentityProvider, SiteIdentityProvider>();
        services.AddSingleton<IHealthReportTransport, AkkaHealthReportTransport>();

        // Site-only components — AddSiteRuntime registers SiteStorageService with SQLite path
        // and site-local repository implementations (IExternalSystemRepository, INotificationRepository)
        var siteDbPath = config["ScadaLink:Database:SiteDbPath"] ?? "site.db";
        services.AddSiteRuntime($"Data Source={siteDbPath}");
        services.AddDataConnectionLayer();
        services.AddStoreAndForward();
        services.AddSiteEventLogging();

        // WP-13: Akka.NET bootstrap via hosted service
        services.AddSingleton<AkkaHostedService>();
        services.AddHostedService(sp => sp.GetRequiredService<AkkaHostedService>());

        // Cluster node status provider for health reports
        services.AddSingleton<IClusterNodeProvider>(sp =>
        {
            var akkaService = sp.GetRequiredService<AkkaHostedService>();
            var nodeOptions = sp.GetRequiredService<Microsoft.Extensions.Options.IOptions<NodeOptions>>().Value;
            var siteRole = $"site-{nodeOptions.SiteId}";
            return new AkkaClusterNodeProvider(akkaService, siteRole);
        });

        // Options binding
        BindSharedOptions(services, config);
        services.Configure<SiteRuntimeOptions>(config.GetSection("ScadaLink:SiteRuntime"));
        services.Configure<DataConnectionOptions>(config.GetSection("ScadaLink:DataConnection"));
        services.Configure<StoreAndForwardOptions>(config.GetSection("ScadaLink:StoreAndForward"));
        services.Configure<SiteEventLogOptions>(config.GetSection("ScadaLink:SiteEventLog"));
    }

    public static void BindSharedOptions(IServiceCollection services, IConfiguration config)
    {
        services.Configure<NodeOptions>(config.GetSection("ScadaLink:Node"));
        services.Configure<ClusterOptions>(config.GetSection("ScadaLink:Cluster"));
        services.Configure<DatabaseOptions>(config.GetSection("ScadaLink:Database"));
        services.Configure<CommunicationOptions>(config.GetSection("ScadaLink:Communication"));
        services.Configure<HealthMonitoringOptions>(config.GetSection("ScadaLink:HealthMonitoring"));
        services.Configure<NotificationOptions>(config.GetSection("ScadaLink:Notification"));
        services.Configure<LoggingOptions>(config.GetSection("ScadaLink:Logging"));
    }
}
