@page "/clusters/{ClusterId}/uns"
@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">UNS structure &middot; <span class="mono">@ClusterId</span></h4>
</div>

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

@if (_areas is null || _lines is null)
{
    <p>Loading…</p>
}
else
{
    <section class="panel notice rise" style="animation-delay:.02s">
        UNS levels: Enterprise (cluster) → Site (cluster) → Area → Line → Equipment. Areas and
        lines are cluster-scoped; equipment hangs under a single line.
    </section>

    <section class="panel rise mt-3" style="animation-delay:.08s">
        <div class="panel-head d-flex align-items-center">
            <span>Areas (level 3) &middot; @_areas.Count</span>
            <a href="/clusters/@ClusterId/uns/areas/new" class="btn btn-sm btn-outline-primary ms-auto">New area</a>
        </div>
        @if (_areas.Count == 0)
        {
            <div style="padding:1rem" class="text-muted">No areas defined.</div>
        }
        else
        {
            <div class="table-wrap">
                <table class="data-table">
                    <thead><tr><th>UnsAreaId</th><th>Name</th><th>Notes</th><th></th></tr></thead>
                    <tbody>
                        @foreach (var a in _areas)
                        {
                            <tr>
                                <td><span class="mono">@a.UnsAreaId</span></td>
                                <td>@a.Name</td>
                                <td class="text-muted small">@(a.Notes ?? "")</td>
                                <td><a href="/clusters/@ClusterId/uns/areas/@a.UnsAreaId" class="btn btn-sm btn-outline-primary">Edit</a></td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        }
    </section>

    <section class="panel rise mt-3" style="animation-delay:.14s">
        <div class="panel-head d-flex align-items-center">
            <span>Lines (level 4) &middot; @_lines.Count</span>
            <a href="/clusters/@ClusterId/uns/lines/new" class="btn btn-sm btn-outline-primary ms-auto">New line</a>
        </div>
        @if (_lines.Count == 0)
        {
            <div style="padding:1rem" class="text-muted">No lines defined.</div>
        }
        else
        {
            <div class="table-wrap">
                <table class="data-table">
                    <thead><tr><th>UnsLineId</th><th>Name</th><th>Area</th><th>Notes</th><th></th></tr></thead>
                    <tbody>
                        @foreach (var l in _lines)
                        {
                            <tr>
                                <td><span class="mono">@l.UnsLineId</span></td>
                                <td>@l.Name</td>
                                <td><span class="mono">@l.UnsAreaId</span></td>
                                <td class="text-muted small">@(l.Notes ?? "")</td>
                                <td><a href="/clusters/@ClusterId/uns/lines/@l.UnsLineId" class="btn btn-sm btn-outline-primary">Edit</a></td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        }
    </section>
}

@code {
    [Parameter] public string ClusterId { get; set; } = "";
    private List<UnsArea>? _areas;
    private List<UnsLine>? _lines;

    protected override async Task OnInitializedAsync()
    {
        await using var db = await DbFactory.CreateDbContextAsync();
        _areas = await db.UnsAreas.AsNoTracking()
            .Where(a => a.ClusterId == ClusterId)
            .OrderBy(a => a.UnsAreaId)
            .ToListAsync();
        var areaIds = _areas.Select(a => a.UnsAreaId).ToList();
        _lines = await db.UnsLines.AsNoTracking()
            .Where(l => areaIds.Contains(l.UnsAreaId))
            .OrderBy(l => l.UnsAreaId).ThenBy(l => l.UnsLineId)
            .ToListAsync();
    }
}
