using Microsoft.Extensions.DependencyInjection;

namespace ZB.MOM.WW.ScadaBridge.InboundAPI;

public static class ServiceCollectionExtensions
{
    /// <summary>
    /// Registers all inbound API services (script executor, route helper, and endpoint filter).
    /// </summary>
    /// <param name="services">The service collection to register into.</param>
    /// <returns>The same <see cref="IServiceCollection"/> to allow chaining.</returns>
    public static IServiceCollection AddInboundAPI(this IServiceCollection services)
    {
        // Auth re-arch (C5): inbound authentication is handled by the shared
        // ZB.MOM.WW.Auth.ApiKeys verifier (Bearer sbk_<keyId>_<secret>), registered by
        // AddZbApiKeyAuth in the Host composition root. The legacy ApiKeyValidator +
        // peppered IApiKeyHasher and the SQL Server ApiKey store were retired, so this
        // extension no longer registers them.
        services.AddSingleton<InboundScriptExecutor>();
        services.AddScoped<RouteHelper>();

        // InboundAPI-017: routed calls go through the IInstanceRouter seam; the
        // production implementation delegates to CommunicationService.
        services.AddScoped<IInstanceRouter, CommunicationServiceInstanceRouter>();

        // InboundAPI-006 / InboundAPI-008: endpoint filter enforcing the request
        // body size cap and active-node gating for POST /api/{methodName}.
        services.AddSingleton<InboundApiEndpointFilter>();

        return services;
    }
}
