using Microsoft.AspNetCore.Builder;

namespace ZB.MOM.WW.ScadaBridge.InboundAPI.Middleware;

/// <summary>
/// <see cref="IApplicationBuilder"/> extensions for wiring
/// <see cref="AuditWriteMiddleware"/> into the ASP.NET Core request pipeline.
/// See <see cref="AuditWriteMiddleware"/> for the placement contract (must run
/// after auth so the resolved API key name is available on
/// <see cref="Microsoft.AspNetCore.Http.HttpContext.Items"/>, and before the
/// inbound-API endpoint handler that owns script execution).
/// </summary>
public static class AuditWriteMiddlewareExtensions
{
    /// <summary>
    /// Registers <see cref="AuditWriteMiddleware"/> in the pipeline.
    /// <see cref="ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services.ICentralAuditWriter"/>
    /// must be registered in DI (typically via <c>AddAuditLog</c>) before this
    /// middleware runs.
    /// </summary>
    /// <param name="app">The application builder to add the middleware to.</param>
    /// <returns>The same <paramref name="app"/> instance, for call chaining.</returns>
    public static IApplicationBuilder UseAuditWriteMiddleware(this IApplicationBuilder app)
    {
        ArgumentNullException.ThrowIfNull(app);
        return app.UseMiddleware<AuditWriteMiddleware>();
    }
}
