using ZB.MOM.WW.Configuration;
using ZB.MOM.WW.OtOpcUa.OpcUaServer;

namespace ZB.MOM.WW.OtOpcUa.Host.Configuration;

/// <summary>
///     Fail-fast startup validator for <see cref="OpcUaApplicationHostOptions"/>, built on the
///     shared <c>ZB.MOM.WW.Configuration</c> <see cref="OptionsValidatorBase{TOptions}"/>. The C#
///     defaults are all valid, so a host with no explicit <c>"OpcUa"</c> section passes untouched;
///     the validator exists to reject explicit prod/env overrides before the OPC UA SDK boots.
///     Identity/transport essentials (<c>ApplicationName</c>, <c>ApplicationUri</c>,
///     <c>PublicHostname</c>, <c>PkiStoreRoot</c>, <c>OpcUaPort</c>) must be present/valid and at
///     least one security profile must be enabled. Optional fields — <c>ApplicationConfigPath</c>,
///     <c>PeerApplicationUris</c>, <c>AutoAcceptUntrustedClientCertificates</c>, and
///     <c>ProductUri</c> — are intentionally not validated. Failure messages carry the real
///     <c>"OpcUa:"</c> section prefix matching the bound configuration section.
/// </summary>
public sealed class OpcUaApplicationHostOptionsValidator : OptionsValidatorBase<OpcUaApplicationHostOptions>
{
    /// <inheritdoc />
    protected override void Validate(ValidationBuilder builder, OpcUaApplicationHostOptions o)
    {
        builder.Required(o.ApplicationName, "OpcUa:ApplicationName");
        builder.Required(o.ApplicationUri, "OpcUa:ApplicationUri");
        builder.Required(o.PublicHostname, "OpcUa:PublicHostname");
        builder.Required(o.PkiStoreRoot, "OpcUa:PkiStoreRoot");
        builder.Port(o.OpcUaPort, "OpcUa:OpcUaPort");
        // EnabledSecurityProfiles is declared as IList<T> — that interface does not derive from
        // IReadOnlyCollection<T>, so it can't bind to MinCount's IReadOnlyCollection<T> parameter
        // directly. ToList() bridges to the shared primitive while preserving the count (and message).
        builder.MinCount(o.EnabledSecurityProfiles?.ToList(), 1, "OpcUa:EnabledSecurityProfiles");
    }
}
