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

/// <summary>
///     A virtual (calculated) tag that lives in the Equipment tree alongside driver tags.
///     Value is produced by the <see cref="Script"/> referenced by <see cref="ScriptId"/>.
/// </summary>
/// <remarks>
///     <para>
///         <see cref="EquipmentId"/> is mandatory — virtual tags are always scoped to an
///         Equipment node (unified Equipment tree, not a separate
///         /Virtual namespace). <see cref="DataType"/> matches the shape used by
///         <c>Tag.DataType</c>.
///     </para>
///     <para>
///         <see cref="ChangeTriggered"/> and <see cref="TimerIntervalMs"/> together realize
///         change + timer evaluation. At least one must produce evaluations; the
///         Core.VirtualTags engine rejects an all-disabled tag at load time.
///     </para>
/// </remarks>
public sealed class VirtualTag
{
    /// <summary>Gets or sets the database row ID (primary key).</summary>
    public Guid VirtualTagRowId { get; set; }

    /// <summary>Gets or sets the stable logical identifier, globally unique in v2.</summary>
    public required string VirtualTagId { get; set; }

    /// <summary>Logical FK to <see cref="Equipment.EquipmentId"/> — owner of this virtual tag.</summary>
    public required string EquipmentId { get; set; }

    /// <summary>Browse name — unique within owning Equipment.</summary>
    public required string Name { get; set; }

    /// <summary>DataType string — same vocabulary as <see cref="Tag.DataType"/>.</summary>
    public required string DataType { get; set; }

    /// <summary>Logical FK to <see cref="Script.ScriptId"/> — the script that computes this tag's value.</summary>
    public required string ScriptId { get; set; }

    /// <summary>Re-evaluate when any referenced input tag changes. Default on.</summary>
    public bool ChangeTriggered { get; set; } = true;

    /// <summary>Timer re-evaluation cadence in milliseconds. <c>null</c> = no timer.</summary>
    public int? TimerIntervalMs { get; set; }

    /// <summary>Gets or sets a value indicating whether this tag's values should be historized.</summary>
    public bool Historize { get; set; }

    /// <summary>Gets or sets a value indicating whether this virtual tag is enabled.</summary>
    public bool Enabled { get; set; } = true;

    /// <summary>Optimistic concurrency token for last-write-wins detection in the v2 live-edit model.</summary>
    public byte[] RowVersion { get; set; } = Array.Empty<byte>();
}
