namespace ScadaLink.Commons.Types.Flattening;

/// <summary>
/// Represents the difference between two FlattenedConfigurations (typically deployed vs current).
/// Used for incremental deployment decisions and change review.
/// </summary>
public sealed record ConfigurationDiff
{
    public string InstanceUniqueName { get; init; } = string.Empty;
    public string? OldRevisionHash { get; init; }
    public string? NewRevisionHash { get; init; }
    public bool HasChanges => AttributeChanges.Count > 0 || AlarmChanges.Count > 0 || ScriptChanges.Count > 0;

    public IReadOnlyList<DiffEntry<ResolvedAttribute>> AttributeChanges { get; init; } = [];
    public IReadOnlyList<DiffEntry<ResolvedAlarm>> AlarmChanges { get; init; } = [];
    public IReadOnlyList<DiffEntry<ResolvedScript>> ScriptChanges { get; init; } = [];
}

/// <summary>
/// A single diff entry showing what changed for a named entity.
/// </summary>
public sealed record DiffEntry<T>
{
    public string CanonicalName { get; init; } = string.Empty;
    public DiffChangeType ChangeType { get; init; }

    /// <summary>
    /// The previous value (null for Added entries).
    /// </summary>
    public T? OldValue { get; init; }

    /// <summary>
    /// The new value (null for Removed entries).
    /// </summary>
    public T? NewValue { get; init; }
}

public enum DiffChangeType
{
    Added,
    Removed,
    Changed
}
