@page "/clusters/{ClusterId}/tags"
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
@rendermode RenderMode.InteractiveServer
@using Microsoft.EntityFrameworkCore
@using ZB.MOM.WW.OtOpcUa.Configuration
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
@inject IDbContextFactory<OtOpcUaConfigDbContext> DbFactory

<div class="d-flex justify-content-between align-items-center mb-3">
    <h4 class="mb-0">Tags &middot; <span class="mono">@ClusterId</span></h4>
    <a href="/clusters/@ClusterId/tags/new" class="btn btn-primary btn-sm">New tag</a>
</div>

<ClusterNav ClusterId="@ClusterId" ActiveTab="tags" />

@if (_rows is null)
{
    <p>Loading…</p>
}
else
{
    <section class="panel notice rise" style="animation-delay:.02s">
        Tags are bound to a driver instance and (optionally) an equipment + poll group. The view
        below shows the first @PageSize tags by Name.
    </section>

    <div class="d-flex align-items-center mb-3 gap-2 mt-3">
        <input type="text" class="form-control form-control-sm" style="max-width:300px"
               placeholder="Filter by name (substring)…"
               @bind="_filter" @bind:event="oninput" />
        <span class="text-muted small">
            Showing @VisibleRows.Count of @_rows.Count
        </span>
    </div>

    <section class="panel rise" style="animation-delay:.08s">
        <div class="panel-head">Tags</div>
        @if (VisibleRows.Count == 0)
        {
            <div style="padding:1rem" class="text-muted">No tags match the current filter.</div>
        }
        else
        {
            <div class="table-wrap">
                <table class="data-table">
                    <thead>
                        <tr>
                            <th>TagId</th>
                            <th>Name</th>
                            <th>Driver</th>
                            <th>Equipment</th>
                            <th>Data type</th>
                            <th>Access</th>
                            <th>Folder</th>
                            <th>Poll group</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var t in VisibleRows)
                        {
                            <tr>
                                <td><span class="mono small">@t.TagId</span></td>
                                <td>@t.Name</td>
                                <td><span class="mono small">@t.DriverInstanceId</span></td>
                                <td>@(t.EquipmentId ?? "—")</td>
                                <td><span class="mono small">@t.DataType</span></td>
                                <td>@t.AccessLevel</td>
                                <td class="text-muted small">@(t.FolderPath ?? "")</td>
                                <td>@(t.PollGroupId ?? "—")</td>
                                <td><a href="/clusters/@ClusterId/tags/@t.TagId" class="btn btn-sm btn-outline-primary">Edit</a></td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        }
    </section>
}

@code {
    private const int PageSize = 200;

    [Parameter] public string ClusterId { get; set; } = "";
    private List<Tag>? _rows;
    private string _filter = "";

    private List<Tag> VisibleRows => (_rows ?? new())
        .Where(t => string.IsNullOrWhiteSpace(_filter)
                 || t.Name.Contains(_filter, StringComparison.OrdinalIgnoreCase))
        .Take(PageSize)
        .ToList();

    protected override async Task OnInitializedAsync()
    {
        await using var db = await DbFactory.CreateDbContextAsync();
        // Tags don't carry ClusterId; resolve via DriverInstance scoping.
        var driverIds = db.DriverInstances.AsNoTracking()
            .Where(d => d.ClusterId == ClusterId)
            .Select(d => d.DriverInstanceId);
        _rows = await db.Tags.AsNoTracking()
            .Where(t => driverIds.Contains(t.DriverInstanceId))
            .OrderBy(t => t.Name)
            .ToListAsync();
    }
}
