@using ZB.MOM.WW.OtOpcUa.Admin.Services

@* Per-section diff renderer — the base used by DiffViewer for every known TableName. Caps
   output at RowCap rows so a pathological draft (e.g. 20k tags churned) can't freeze the
   Blazor render; overflow banner tells operator how many rows were hidden. *@

<div class="card mb-3">
    <div class="card-header d-flex justify-content-between align-items-center">
        <div>
            <strong>@Title</strong>
            <small class="text-muted ms-2">@Description</small>
        </div>
        <div>
            @if (_added > 0) { <span class="badge bg-success me-1">+@_added</span> }
            @if (_removed > 0) { <span class="badge bg-danger me-1">−@_removed</span> }
            @if (_modified > 0) { <span class="badge bg-warning text-dark me-1">~@_modified</span> }
            @if (_total == 0) { <span class="badge bg-secondary">no changes</span> }
        </div>
    </div>
    @if (_total == 0)
    {
        <div class="card-body text-muted small">No changes in this section.</div>
    }
    else
    {
        @if (_total > RowCap)
        {
            <div class="alert alert-warning mb-0 small rounded-0">
                Showing the first @RowCap of @_total rows — cap protects the browser from megabyte-class
                diffs. Inspect the remainder via the SQL <code>sp_ComputeGenerationDiff</code> directly.
            </div>
        }
        <div class="table-responsive" style="max-height: 400px; overflow-y: auto;">
            <table class="table table-sm table-hover mb-0">
                <thead class="table-light">
                    <tr><th>LogicalId</th><th style="width: 120px;">Change</th></tr>
                </thead>
                <tbody>
                @foreach (var r in _visibleRows)
                {
                    <tr>
                        <td><code>@r.LogicalId</code></td>
                        <td>
                            @switch (r.ChangeKind)
                            {
                                case "Added":    <span class="badge bg-success">@r.ChangeKind</span> break;
                                case "Removed":  <span class="badge bg-danger">@r.ChangeKind</span> break;
                                case "Modified": <span class="badge bg-warning text-dark">@r.ChangeKind</span> break;
                                default:         <span class="badge bg-secondary">@r.ChangeKind</span> break;
                            }
                        </td>
                    </tr>
                }
                </tbody>
            </table>
        </div>
    }
</div>

@code {
    /// <summary>Default row-cap per section — matches task #156's acceptance criterion.</summary>
    public const int DefaultRowCap = 1000;

    [Parameter, EditorRequired] public string Title { get; set; } = string.Empty;
    [Parameter] public string Description { get; set; } = string.Empty;
    [Parameter, EditorRequired] public IReadOnlyList<DiffRow> Rows { get; set; } = [];
    [Parameter] public int RowCap { get; set; } = DefaultRowCap;

    private int _total;
    private int _added;
    private int _removed;
    private int _modified;
    private List<DiffRow> _visibleRows = [];

    protected override void OnParametersSet()
    {
        _total = Rows.Count;
        _added = 0; _removed = 0; _modified = 0;
        foreach (var r in Rows)
        {
            switch (r.ChangeKind)
            {
                case "Added":    _added++;    break;
                case "Removed":  _removed++;  break;
                case "Modified": _modified++; break;
            }
        }
        _visibleRows = _total > RowCap ? Rows.Take(RowCap).ToList() : Rows.ToList();
    }
}
