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

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

/// <summary>
///     v3 raw-only tag (signal). A tag is authored ONCE, under a <see cref="Device"/> in the Raw
///     tree; its identity is its <c>RawPath</c>
///     (<c>&lt;Folder/…&gt;/&lt;Driver&gt;/&lt;Device&gt;/&lt;TagGroup/…&gt;/&lt;Name&gt;</c>). UNS
///     surfaces it by reference (<see cref="UnsTagReference"/>), never by re-authoring. The tag's
///     driver-specific address lives inside <see cref="TagConfig"/> as an ordinary field (what the
///     driver dials), not as system identity.
/// </summary>
public sealed class Tag
{
    /// <summary>Gets or sets the unique database row identifier for the tag (PK).</summary>
    public Guid TagRowId { get; set; }

    /// <summary>Gets or sets the stable logical tag identifier (unique fleet-wide).</summary>
    public required string TagId { get; set; }

    /// <summary>v3: required logical FK to <see cref="Device.DeviceId"/>. Every tag lives under a device.</summary>
    public required string DeviceId { get; set; }

    /// <summary>
    ///     Optional logical FK to a containing <see cref="TagGroup.TagGroupId"/>;
    ///     <see langword="null"/> = the tag sits directly under its device. Contributes the middle
    ///     segments of the tag's <c>RawPath</c>.
    /// </summary>
    public string? TagGroupId { get; set; }

    /// <summary>Gets or sets the tag name. A RawPath segment: no <c>/</c>, no lead/trail whitespace.</summary>
    public required string Name { get; set; }

    /// <summary>OPC UA built-in type name (Boolean / Int32 / Float / etc.).</summary>
    public required string DataType { get; set; }

    /// <summary>
    /// Gets or sets the access level for this tag.
    /// </summary>
    public required TagAccessLevel AccessLevel { get; set; }

    /// <summary>Opt-in for write retry eligibility (R2 resilience contract — retained in v3).</summary>
    public bool WriteIdempotent { get; set; }

    /// <summary>
    /// Gets or sets the poll group identifier for batching read/write operations.
    /// </summary>
    public string? PollGroupId { get; set; }

    /// <summary>Register address / scaling / poll group / byte-order / etc. — schemaless per driver type.</summary>
    public required string TagConfig { get; set; }

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