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

/// <summary>
/// Thrown when the post-apply semantic validation pass detects that the merged
/// target configuration would not be deployable — e.g. a template script
/// references a SharedScript or ExternalSystem that exists in neither the
/// bundle nor the (post-merge) target database.
/// <para>
/// The exception is caught inside <see cref="BundleImporter.ApplyAsync"/> to
/// roll back the transaction, emit a <c>BundleImportFailed</c> audit row, and
/// re-throw to the caller so the UI can surface the specific errors. It is
/// deliberately distinct from <see cref="InvalidOperationException"/> so the
/// caller can distinguish "your bundle is bad" from "the import infra is bad".
/// </para>
/// </summary>
public sealed class SemanticValidationException : Exception
{
    /// <summary>Gets the list of semantic validation error messages that caused this exception.</summary>
    public IReadOnlyList<string> Errors { get; }

    /// <summary>
    /// Initializes a new <see cref="SemanticValidationException"/> with the given error list.
    /// </summary>
    /// <param name="errors">The list of validation error messages to include in the exception.</param>
    public SemanticValidationException(IReadOnlyList<string> errors)
        : base(BuildMessage(errors))
    {
        Errors = errors ?? throw new ArgumentNullException(nameof(errors));
    }

    private static string BuildMessage(IReadOnlyList<string> errors)
    {
        if (errors is null || errors.Count == 0)
        {
            return "Bundle semantic validation failed.";
        }
        return "Bundle semantic validation failed: " + string.Join("; ", errors);
    }
}
