using ZB.MOM.WW.OtOpcUa.AdminUI.Uns.TagEditors;

namespace ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers.DeviceForms;

/// <summary>
///     Device model for the AVEVA Galaxy driver. Galaxy connects to a SINGLE mxaccessgw gateway configured
///     on the <b>driver</b> (nested Gateway/MxAccess records) — there is no flat per-device connection
///     endpoint to author, so this model authors no endpoint keys. It simply round-trips the DeviceConfig
///     JSON verbatim (preserving any keys) so the device row stays valid. (Flagged: Galaxy has no v3
///     endpoint→DeviceConfig split; its connection lives on the driver config.)
/// </summary>
public sealed class GalaxyDeviceModel
{
    private System.Text.Json.Nodes.JsonObject _bag = new();

    /// <summary>Loads a model from a DeviceConfig JSON string, retaining every original key.</summary>
    /// <param name="json">The raw DeviceConfig JSON string, or null for a new/empty device.</param>
    /// <returns>The populated <see cref="GalaxyDeviceModel"/>.</returns>
    public static GalaxyDeviceModel FromJson(string? json)
        => new() { _bag = TagConfigJson.ParseOrNew(json) };

    /// <summary>Serializes the (preserved) DeviceConfig back to a JSON string.</summary>
    /// <returns>The serialised DeviceConfig JSON string.</returns>
    public string ToJson() => TagConfigJson.Serialize(_bag);

    /// <summary>Validation hook; Galaxy device rows carry no endpoint, so always valid.</summary>
    /// <returns><c>null</c> — no per-device endpoint to validate.</returns>
    public string? Validate() => null;
}
