namespace ZB.MOM.WW.ScadaBridge.Transport.Import;

/// <summary>
/// T-003: thrown by <see cref="BundleImporter.LoadAsync"/> when an encrypted bundle has
/// exceeded the configured failed-unlock attempt limit
/// (<see cref="TransportOptions.MaxUnlockAttemptsPerSession"/>). The lockout is tracked
/// server-side keyed by <c>BundleManifest.ContentHash</c>, so a second tab / CLI caller
/// re-uploading the same bytes hits the same counter and cannot side-step the limit.
/// </summary>
public sealed class BundleLockedException : Exception
{
    /// <summary>Number of recorded unlock failures for this bundle.</summary>
    public int FailedAttempts { get; }

    /// <summary>SHA-256 (hex) of the bundle's content bytes, the lockout's tracking key.</summary>
    public string BundleContentHash { get; }

    /// <summary>
    /// Initializes a new <see cref="BundleLockedException"/>.
    /// </summary>
    /// <param name="bundleContentHash">SHA-256 hex from <c>BundleManifest.ContentHash</c>.</param>
    /// <param name="failedAttempts">Number of failures recorded against this bundle.</param>
    public BundleLockedException(string bundleContentHash, int failedAttempts)
        : base(
            $"Bundle is locked after {failedAttempts} failed unlock attempts. "
            + "Wait for the lockout window to expire or re-export the bundle to obtain a new content hash.")
    {
        BundleContentHash = bundleContentHash ?? throw new ArgumentNullException(nameof(bundleContentHash));
        FailedAttempts = failedAttempts;
    }
}
