using Microsoft.Extensions.Options;
using ZB.MOM.WW.ScadaBridge.StoreAndForward;

namespace ZB.MOM.WW.ScadaBridge.Host;

/// <summary>
/// Host-side adapter implementing the
/// optional <see cref="IStoreAndForwardSiteContext"/> the Store-and-Forward
/// service consults to stamp cached-call audit telemetry with the site id.
/// </summary>
/// <remarks>
/// Forwards <see cref="NodeOptions.SiteId"/> verbatim — the same value
/// <see cref="SiteIdentityProvider"/> exposes to HealthMonitoring. Defined as
/// a separate adapter (rather than reusing <see cref="SiteIdentityProvider"/>)
/// to avoid pulling HealthMonitoring into the StoreAndForward project's
/// dependency graph, which would create a project-reference cycle.
/// </remarks>
public class StoreAndForwardSiteContext : IStoreAndForwardSiteContext
{
    /// <inheritdoc />
    public string SiteId { get; }

    /// <summary>Initializes a new instance of <see cref="StoreAndForwardSiteContext"/>.</summary>
    /// <param name="nodeOptions">Node options supplying the site identifier.</param>
    public StoreAndForwardSiteContext(IOptions<NodeOptions> nodeOptions)
    {
        // NodeOptions.SiteId is nullable; SiteServiceRegistration ONLY adds
        // this binding on the site role, so a non-null site id is expected
        // here. Mirror SiteIdentityProvider's hard fail so a missing site id
        // surfaces at composition time rather than at the first cached call.
        SiteId = nodeOptions.Value.SiteId
            ?? throw new InvalidOperationException(
                "ScadaBridge:Node:SiteId is required for the site role's StoreAndForward wiring.");
    }
}
