@page "/clusters/{ClusterId}/namespaces"
@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">Namespaces &middot; <span class="mono">@ClusterId</span></h4>
    <a href="/clusters/@ClusterId/namespaces/new" class="btn btn-primary btn-sm">New namespace</a>
</div>

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

@if (_rows is null)
{
    <p>Loading…</p>
}
else
{
    <section class="panel notice rise" style="animation-delay:.02s">
        Namespaces are content (decision #123) — they're served at the OPC UA endpoint and bound
        to driver instances. NamespaceUri must be unique fleet-wide.
    </section>

    <section class="panel rise mt-3" style="animation-delay:.08s">
        <div class="panel-head">@_rows.Count namespace@(_rows.Count == 1 ? "" : "s")</div>
        @if (_rows.Count == 0)
        {
            <div style="padding:1rem" class="text-muted">No namespaces defined for this cluster.</div>
        }
        else
        {
            <div class="table-wrap">
                <table class="data-table">
                    <thead>
                        <tr>
                            <th>NamespaceId</th>
                            <th>Kind</th>
                            <th>URI</th>
                            <th>Status</th>
                            <th>Notes</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var n in _rows)
                        {
                            <tr>
                                <td><span class="mono">@n.NamespaceId</span></td>
                                <td>@n.Kind</td>
                                <td><span class="mono small">@n.NamespaceUri</span></td>
                                <td>
                                    @if (n.Enabled) { <span class="chip chip-ok">Enabled</span> }
                                    else { <span class="chip chip-idle">Disabled</span> }
                                </td>
                                <td class="text-muted small">@(n.Notes ?? "")</td>
                                <td><a href="/clusters/@ClusterId/namespaces/@n.NamespaceId" class="btn btn-sm btn-outline-primary">Edit</a></td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        }
    </section>
}

@code {
    [Parameter] public string ClusterId { get; set; } = "";
    private List<Namespace>? _rows;

    protected override async Task OnInitializedAsync()
    {
        await using var db = await DbFactory.CreateDbContextAsync();
        _rows = await db.Namespaces.AsNoTracking()
            .Where(n => n.ClusterId == ClusterId)
            .OrderBy(n => n.NamespaceId)
            .ToListAsync();
    }
}
