@page "/clusters/{ClusterId}/draft/{GenerationId:long}"
@using ZB.MOM.WW.OtOpcUa.Admin.Services
@using ZB.MOM.WW.OtOpcUa.Configuration.Validation
@inject GenerationService GenerationSvc
@inject DraftValidationService ValidationSvc
@inject NavigationManager Nav

<div class="d-flex justify-content-between align-items-center mb-3">
    <div>
        <h1 class="mb-0">Draft editor</h1>
        <small class="text-muted">Cluster <code>@ClusterId</code> · generation @GenerationId</small>
    </div>
    <div>
        <a class="btn btn-outline-secondary" href="/clusters/@ClusterId">Back to cluster</a>
        <a class="btn btn-outline-primary ms-2" href="/clusters/@ClusterId/draft/@GenerationId/diff">View diff</a>
        <button class="btn btn-primary ms-2" disabled="@(_errors.Count != 0 || _busy)" @onclick="PublishAsync">Publish</button>
    </div>
</div>

<ul class="nav nav-tabs mb-3">
    <li class="nav-item"><button class="nav-link @Active("equipment")" @onclick='() => _tab = "equipment"'>Equipment</button></li>
    <li class="nav-item"><button class="nav-link @Active("uns")" @onclick='() => _tab = "uns"'>UNS</button></li>
    <li class="nav-item"><button class="nav-link @Active("namespaces")" @onclick='() => _tab = "namespaces"'>Namespaces</button></li>
    <li class="nav-item"><button class="nav-link @Active("drivers")" @onclick='() => _tab = "drivers"'>Drivers</button></li>
    <li class="nav-item"><button class="nav-link @Active("acls")" @onclick='() => _tab = "acls"'>ACLs</button></li>
    <li class="nav-item"><button class="nav-link @Active("scripts")" @onclick='() => _tab = "scripts"'>Scripts</button></li>
</ul>

<div class="row">
    <div class="col-md-8">
        @if (_tab == "equipment") { <EquipmentTab GenerationId="@GenerationId" ClusterId="@ClusterId"/> }
        else if (_tab == "uns") { <UnsTab GenerationId="@GenerationId" ClusterId="@ClusterId"/> }
        else if (_tab == "namespaces") { <NamespacesTab GenerationId="@GenerationId" ClusterId="@ClusterId"/> }
        else if (_tab == "drivers") { <DriversTab GenerationId="@GenerationId" ClusterId="@ClusterId"/> }
        else if (_tab == "acls") { <AclsTab GenerationId="@GenerationId" ClusterId="@ClusterId"/> }
        else if (_tab == "scripts") { <ScriptsTab GenerationId="@GenerationId" ClusterId="@ClusterId"/> }
    </div>
    <div class="col-md-4">
        <div class="card sticky-top">
            <div class="card-header d-flex justify-content-between align-items-center">
                <strong>Validation</strong>
                <button class="btn btn-sm btn-outline-secondary" @onclick="RevalidateAsync">Re-run</button>
            </div>
            <div class="card-body">
                @if (_validating) { <p class="text-muted">Checking…</p> }
                else if (_errors.Count == 0) { <div class="alert alert-success mb-0">No validation errors — safe to publish.</div> }
                else
                {
                    <div class="alert alert-danger mb-2">@_errors.Count error@(_errors.Count == 1 ? "" : "s")</div>
                    <ul class="list-unstyled">
                        @foreach (var e in _errors)
                        {
                            <li class="mb-2">
                                <span class="badge bg-danger me-1">@e.Code</span>
                                <small>@e.Message</small>
                                @if (!string.IsNullOrEmpty(e.Context)) { <div class="text-muted"><code>@e.Context</code></div> }
                            </li>
                        }
                    </ul>
                }
            </div>
        </div>

        @if (_publishError is not null) { <div class="alert alert-danger mt-3">@_publishError</div> }
    </div>
</div>

@code {
    [Parameter] public string ClusterId { get; set; } = string.Empty;
    [Parameter] public long GenerationId { get; set; }

    private string _tab = "equipment";
    private List<ValidationError> _errors = [];
    private bool _validating;
    private bool _busy;
    private string? _publishError;

    private string Active(string k) => _tab == k ? "active" : string.Empty;

    protected override async Task OnParametersSetAsync() => await RevalidateAsync();

    private async Task RevalidateAsync()
    {
        _validating = true;
        try
        {
            var errors = await ValidationSvc.ValidateAsync(GenerationId, CancellationToken.None);
            _errors = errors.ToList();
        }
        finally { _validating = false; }
    }

    private async Task PublishAsync()
    {
        _busy = true;
        _publishError = null;
        try
        {
            await GenerationSvc.PublishAsync(ClusterId, GenerationId, notes: "Published via Admin UI", CancellationToken.None);
            Nav.NavigateTo($"/clusters/{ClusterId}");
        }
        catch (Exception ex) { _publishError = ex.Message; }
        finally { _busy = false; }
    }
}
