namespace ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;

/// <summary>
/// Result of pre-deployment or on-demand validation with categorized errors and warnings.
/// </summary>
public sealed record ValidationResult
{
    /// <summary>True when there are no validation errors.</summary>
    public bool IsValid => Errors.Count == 0;
    /// <summary>Validation errors that block the operation.</summary>
    public IReadOnlyList<ValidationEntry> Errors { get; init; } = [];
    /// <summary>Non-blocking validation warnings.</summary>
    public IReadOnlyList<ValidationEntry> Warnings { get; init; } = [];

    /// <summary>Returns a result with no errors or warnings.</summary>
    /// <returns>A <see cref="ValidationResult"/> with empty error and warning lists.</returns>
    public static ValidationResult Success() => new();

    /// <summary>Returns a result containing the given errors.</summary>
    /// <param name="errors">The validation errors to include.</param>
    /// <returns>A <see cref="ValidationResult"/> whose <see cref="Errors"/> list contains the supplied entries.</returns>
    public static ValidationResult FromErrors(params ValidationEntry[] errors) =>
        new() { Errors = errors };

    /// <summary>Merges multiple validation results into a single combined result.</summary>
    /// <param name="results">The results to merge.</param>
    /// <returns>A new <see cref="ValidationResult"/> with all errors and warnings from every supplied result combined.</returns>
    public static ValidationResult Merge(params ValidationResult[] results)
    {
        var errors = new List<ValidationEntry>();
        var warnings = new List<ValidationEntry>();
        foreach (var r in results)
        {
            errors.AddRange(r.Errors);
            warnings.AddRange(r.Warnings);
        }
        return new ValidationResult { Errors = errors, Warnings = warnings };
    }
}

/// <summary>
/// A single validation error or warning with category and context.
/// </summary>
public sealed record ValidationEntry
{
    /// <summary>The category classifying the kind of validation failure.</summary>
    public ValidationCategory Category { get; init; }
    /// <summary>Human-readable description of the validation issue.</summary>
    public string Message { get; init; } = string.Empty;

    /// <summary>
    /// The canonical name of the entity that caused the validation issue, if applicable.
    /// </summary>
    public string? EntityName { get; init; }

    /// <summary>Creates an error entry with the given category, message, and optional entity name.</summary>
    /// <param name="category">The validation category.</param>
    /// <param name="message">The error message.</param>
    /// <param name="entityName">The canonical name of the entity that caused the error, if any.</param>
    /// <returns>A new <see cref="ValidationEntry"/> representing an error.</returns>
    public static ValidationEntry Error(ValidationCategory category, string message, string? entityName = null) =>
        new() { Category = category, Message = message, EntityName = entityName };

    /// <summary>Creates a warning entry with the given category, message, and optional entity name.</summary>
    /// <param name="category">The validation category.</param>
    /// <param name="message">The warning message.</param>
    /// <param name="entityName">The canonical name of the entity that triggered the warning, if any.</param>
    /// <returns>A new <see cref="ValidationEntry"/> representing a warning.</returns>
    public static ValidationEntry Warning(ValidationCategory category, string message, string? entityName = null) =>
        new() { Category = category, Message = message, EntityName = entityName };
}

public enum ValidationCategory
{
    FlatteningFailure,
    NamingCollision,
    ScriptCompilation,
    AlarmTriggerReference,
    ScriptTriggerReference,
    ConnectionBinding,
    CallTargetNotFound,
    ParameterMismatch,
    ReturnTypeMismatch,
    TriggerOperandType,
    OnTriggerScriptNotFound,
    CrossCallViolation,
    MissingMetadata,
    ConnectionConfig,
    NativeAlarmSourceInvalid
}
