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

/// <summary>One driver instance in a cluster's generation. JSON config is schemaless per-driver-type.</summary>
public sealed class DriverInstance
{
    /// <summary>Gets or sets the row ID for this driver instance.</summary>
    public Guid DriverInstanceRowId { get; set; }

    /// <summary>Gets or sets the unique driver instance identifier.</summary>
    public required string DriverInstanceId { get; set; }

    /// <summary>Gets or sets the cluster ID this driver instance belongs to.</summary>
    public required string ClusterId { get; set; }

    /// <summary>
    ///     v3: logical FK to a containing <see cref="RawFolder.RawFolderId"/>; <see langword="null"/>
    ///     = the driver sits at the cluster root of the Raw tree. Contributes the leading segments of
    ///     every child tag's <c>RawPath</c>.
    /// </summary>
    public string? RawFolderId { get; set; }

    /// <summary>Gets or sets the friendly name of this driver instance. A RawPath segment.</summary>
    public required string Name { get; set; }

    /// <summary>Galaxy | Modbus | AbCip | AbLegacy | S7 | TwinCat | Focas | OpcUaClient</summary>
    public required string DriverType { get; set; }

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

    /// <summary>Schemaless per-driver-type JSON config. Validated against registered JSON schema at draft-publish time.</summary>
    public required string DriverConfig { get; set; }

    /// <summary>
    ///     Optional per-instance overrides for the Phase 6.1 shared Polly resilience pipeline.
    ///     Null = use the driver's tier defaults. When populated, expected shape:
    ///     <code>
    ///     {
    ///       "capabilityPolicies": {
    ///         "Read":  { "timeoutSeconds": 5, "retryCount": 5, "breakerFailureThreshold": 3 },
    ///         "Write": { "timeoutSeconds": 5, "retryCount": 0, "breakerFailureThreshold": 5 }
    ///       }
    ///     }
    ///     </code>
    ///     Parsed at startup by <c>DriverResilienceOptionsParser</c>; every key is optional +
    ///     unrecognised keys are ignored so future shapes land without a migration.
    /// </summary>
    public string? ResilienceConfig { 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>();

    /// <summary>Gets or sets the related server cluster for navigation.</summary>
    public ServerCluster? Cluster { get; set; }
}
