using ZB.MOM.WW.OtOpcUa.Core.Abstractions;

namespace ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.IntegrationTests;

/// <summary>
///     Test helper bridging the v2 authoring shape (a typed <see cref="AbLegacyTagDefinition"/>) to the v3
///     delivery shape (<see cref="RawTagEntry"/>). Under v3 the driver no longer takes pre-declared
///     definitions — the deploy artifact hands it authored raw tags (RawPath + TagConfig blob + owning
///     DeviceName), and the driver rebuilds the typed definitions via
///     <see cref="AbLegacyTagDefinitionFactory"/>. These integration smoke tests keep expressing their
///     intent as typed definitions; this helper serialises each back to its TagConfig blob (via
///     <see cref="AbLegacyTagDefinitionFactory.ToTagConfig"/>) and packages it as a <see cref="RawTagEntry"/>
///     whose RawPath is the def's <c>Name</c> and whose <see cref="RawTagEntry.DeviceName"/> is the def's
///     <c>DeviceHostAddress</c> — so the round-trip through the real mapper reproduces the same definition
///     the test authored, keyed by the same name the read/write/subscribe call sites use and routed to the
///     same device (the driver matches DeviceName against a configured device's Name or HostAddress).
///     Mirrors the unit-test project's helper of the same name.
/// </summary>
internal static class AbLegacyRawTags
{
    /// <summary>Serialises one typed definition into its <see cref="RawTagEntry"/> (RawPath = def.Name, DeviceName = def.DeviceHostAddress).</summary>
    /// <param name="def">The typed definition to convert.</param>
    /// <returns>The equivalent <see cref="RawTagEntry"/>.</returns>
    public static RawTagEntry Entry(AbLegacyTagDefinition def)
        => new(def.Name, AbLegacyTagDefinitionFactory.ToTagConfig(def), def.WriteIdempotent, DeviceName: def.DeviceHostAddress);

    /// <summary>Serialises typed definitions into <see cref="RawTagEntry"/> records.</summary>
    /// <param name="defs">The typed definitions to convert (array, params, or a collection expression).</param>
    /// <returns>The equivalent raw-tag entries.</returns>
    public static IReadOnlyList<RawTagEntry> Entries(params AbLegacyTagDefinition[] defs)
        => [.. defs.Select(Entry)];
}
