using ScadaLink.AuditLog;
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;
///
/// Extracted site-role DI registrations so both Program.cs and tests
/// use the same composition root.
///
public static class SiteServiceRegistration
{
public static void Configure(IServiceCollection services, IConfiguration config)
{
// Shared components
services.AddClusterInfrastructure();
services.AddCommunication();
services.AddSiteHealthMonitoring();
services.AddExternalSystemGateway();
// AddNotificationService() is intentionally NOT registered on the site path.
// Sites no longer deliver notifications over SMTP — a buffered notification is
// forwarded to the central cluster (via NotificationForwarder / SiteCommunicationActor),
// and central owns SMTP delivery through the Notification Outbox. The SMTP machinery
// (OAuth2TokenService, ISmtpClientWrapper, INotificationDeliveryService) has no
// consumer on a site node.
// Health report transport: sends SiteHealthReport to SiteCommunicationActor via Akka
services.AddSingleton();
services.AddSingleton();
// 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();
// Audit Log (#23) — site-side hot-path writer + telemetry collaborators.
// The SiteAuditTelemetryActor itself is registered by AkkaHostedService
// in the site-role block; this call wires every DI dependency it (and
// ScriptRuntimeContext, when Bundle F lands) reaches for.
services.AddAuditLog(config);
// WP-13: Akka.NET bootstrap via hosted service
services.AddSingleton();
services.AddHostedService(sp => sp.GetRequiredService());
// Cluster node status provider for health reports
services.AddSingleton(sp =>
{
var akkaService = sp.GetRequiredService();
var nodeOptions = sp.GetRequiredService>().Value;
var siteRole = $"site-{nodeOptions.SiteId}";
return new AkkaClusterNodeProvider(akkaService, siteRole);
});
// Options binding
BindSharedOptions(services, config);
services.Configure(config.GetSection("ScadaLink:SiteRuntime"));
services.Configure(config.GetSection("ScadaLink:DataConnection"));
services.Configure(config.GetSection("ScadaLink:StoreAndForward"));
services.Configure(config.GetSection("ScadaLink:SiteEventLog"));
}
public static void BindSharedOptions(IServiceCollection services, IConfiguration config)
{
services.Configure(config.GetSection("ScadaLink:Node"));
services.Configure(config.GetSection("ScadaLink:Cluster"));
services.Configure(config.GetSection("ScadaLink:Database"));
services.Configure(config.GetSection("ScadaLink:Communication"));
services.Configure(config.GetSection("ScadaLink:HealthMonitoring"));
services.Configure(config.GetSection("ScadaLink:Notification"));
services.Configure(config.GetSection("ScadaLink:Logging"));
}
}