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>
    ///     Logical FK to <see cref="Namespace.NamespaceId"/>. Same-cluster binding enforced by
    ///     <c>sp_ValidateDraft</c> per decision #122: Namespace.ClusterId must equal DriverInstance.ClusterId.
    /// </summary>
    public required string NamespaceId { get; set; }

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

    /// <summary>Galaxy | ModbusTcp | 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 (decision #91).</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 (decision #143). When populated, expected shape:
    ///     <code>
    ///     {
    ///       "bulkheadMaxConcurrent": 16,
    ///       "bulkheadMaxQueue": 64,
    ///       "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; }
}
