@page "/clusters/{ClusterId}/nodes/new"
@page "/clusters/{ClusterId}/nodes/{NodeId}"
@* ClusterNode CRUD. ApplicationUri is fleet-wide unique — the EF unique index enforces this
   at SaveChanges. ServiceLevelBase defaults: 200 primary, 150 secondary. *@
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
@rendermode RenderMode.InteractiveServer
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.EntityFrameworkCore
@using System.ComponentModel.DataAnnotations
@using ZB.MOM.WW.OtOpcUa.Configuration
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
@inject IDbContextFactory<OtOpcUaConfigDbContext> DbFactory
@inject NavigationManager Nav
@inject AuthenticationStateProvider AuthState

<div class="d-flex justify-content-between align-items-center mb-3">
    <h4 class="mb-0">@(IsNew ? "New node" : "Edit node") &middot; <span class="mono">@ClusterId</span></h4>
    <a href="/clusters/@ClusterId" class="btn btn-outline-secondary btn-sm">Cancel</a>
</div>

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

@if (!_loaded)
{
    <p>Loading…</p>
}
else if (!IsNew && _existing is null)
{
    <section class="panel notice rise" style="animation-delay:.02s">
        Node <span class="mono">@NodeId</span> was not found in cluster <span class="mono">@ClusterId</span>.
    </section>
}
else
{
    <EditForm Model="_form" OnValidSubmit="SubmitAsync" FormName="nodeEdit">
        <DataAnnotationsValidator />
        <section class="panel rise" style="animation-delay:.02s">
            <div class="panel-head">Identity</div>
            <div style="padding:1rem">
                <div class="mb-3">
                    <label class="form-label" for="nodeId">NodeId</label>
                    <InputText id="nodeId" @bind-Value="_form.NodeId" disabled="@(!IsNew)"
                               class="form-control form-control-sm mono"
                               placeholder="LINE3-OPCUA-A" />
                </div>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label" for="host">Host</label>
                        <InputText id="host" @bind-Value="_form.Host" class="form-control form-control-sm mono"
                                   placeholder="line3-opc-a.plant.local" />
                    </div>
                    <div class="col-md-3 mb-3">
                        <label class="form-label" for="port">OPC UA port</label>
                        <InputNumber id="port" @bind-Value="_form.OpcUaPort" class="form-control form-control-sm" />
                    </div>
                    <div class="col-md-3 mb-3">
                        <label class="form-label" for="dashport">Dashboard port</label>
                        <InputNumber id="dashport" @bind-Value="_form.DashboardPort" class="form-control form-control-sm" />
                    </div>
                </div>
                <div class="mb-3">
                    <label class="form-label" for="uri">ApplicationUri</label>
                    <InputText id="uri" @bind-Value="_form.ApplicationUri" class="form-control form-control-sm mono"
                               placeholder="urn:zb:warsaw-west:line3:opc-a" />
                    <div class="form-text">Must be unique fleet-wide. Clients pin trust here — never silently rewrite based on Host.</div>
                </div>
            </div>
        </section>

        <section class="panel rise mt-3" style="animation-delay:.08s">
            <div class="panel-head">Redundancy + behaviour</div>
            <div style="padding:1rem">
                <div class="row">
                    <div class="col-md-4 mb-3">
                        <label class="form-label" for="slbase">ServiceLevel base</label>
                        <InputNumber id="slbase" @bind-Value="_form.ServiceLevelBase" class="form-control form-control-sm" />
                        <div class="form-text">200 = primary preference, 150 = secondary preference. Live ServiceLevel adjusts down on faults.</div>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label class="form-label">Enabled</label>
                        <div class="form-check form-switch">
                            <InputCheckbox @bind-Value="_form.Enabled" class="form-check-input" />
                            <label class="form-check-label">Join the Akka cluster + serve endpoints</label>
                        </div>
                    </div>
                </div>
                <div class="mb-3">
                    <label class="form-label" for="overrides">Driver config overrides (JSON, optional)</label>
                    <InputTextArea id="overrides" @bind-Value="_form.DriverConfigOverridesJson" rows="6"
                                   class="form-control form-control-sm mono"
                                   placeholder='{ "drv-modbus-line3-01": { "endpoint": "10.0.0.43:502" } }' />
                    <div class="form-text">Per-node merge over cluster-level <span class="mono">DriverInstance.DriverConfig</span>. Minimal by design — heavy node-specific config is a smell.</div>
                </div>
            </div>
        </section>

        @if (!string.IsNullOrWhiteSpace(_error))
        {
            <div class="panel notice mt-3" style="border-color:var(--alert)">@_error</div>
        }

        <div class="mt-3 d-flex gap-2">
            <button type="submit" class="btn btn-primary" disabled="@_busy">
                @if (_busy) { <span class="spinner-border spinner-border-sm me-1"></span> }
                @(IsNew ? "Create" : "Save changes")
            </button>
            <a href="/clusters/@ClusterId" class="btn btn-outline-secondary">Cancel</a>
            @if (!IsNew)
            {
                <button type="button" class="btn btn-outline-danger ms-auto" @onclick="DeleteAsync" disabled="@_busy">
                    Delete
                </button>
            }
        </div>
    </EditForm>
}

@code {
    [Parameter] public string ClusterId { get; set; } = "";
    [Parameter] public string? NodeId { get; set; }

    private bool IsNew => string.IsNullOrEmpty(NodeId);

    private FormModel _form = new();
    private ClusterNode? _existing;
    private bool _loaded;
    private bool _busy;
    private string? _error;

    protected override async Task OnInitializedAsync()
    {
        if (IsNew)
        {
            _form = new FormModel
            {
                NodeId = "",
                Host = "",
                OpcUaPort = 4840,
                DashboardPort = 8081,
                ApplicationUri = "",
                ServiceLevelBase = 200,
                Enabled = true,
            };
        }
        else
        {
            await using var db = await DbFactory.CreateDbContextAsync();
            _existing = await db.ClusterNodes.AsNoTracking()
                .FirstOrDefaultAsync(n => n.ClusterId == ClusterId && n.NodeId == NodeId);
            if (_existing is not null)
            {
                _form = new FormModel
                {
                    NodeId = _existing.NodeId,
                    Host = _existing.Host,
                    OpcUaPort = _existing.OpcUaPort,
                    DashboardPort = _existing.DashboardPort,
                    ApplicationUri = _existing.ApplicationUri,
                    ServiceLevelBase = _existing.ServiceLevelBase,
                    Enabled = _existing.Enabled,
                    DriverConfigOverridesJson = _existing.DriverConfigOverridesJson,
                };
            }
        }
        _loaded = true;
    }

    private async Task SubmitAsync()
    {
        _busy = true;
        _error = null;
        try
        {
            if (!string.IsNullOrWhiteSpace(_form.DriverConfigOverridesJson))
            {
                try { using var _ = System.Text.Json.JsonDocument.Parse(_form.DriverConfigOverridesJson); }
                catch { _error = "DriverConfigOverridesJson is not valid JSON."; return; }
            }

            await using var db = await DbFactory.CreateDbContextAsync();
            if (IsNew)
            {
                if (await db.ClusterNodes.AnyAsync(n => n.NodeId == _form.NodeId))
                {
                    _error = $"Node '{_form.NodeId}' already exists.";
                    return;
                }
                var auth = await AuthState.GetAuthenticationStateAsync();
                db.ClusterNodes.Add(new ClusterNode
                {
                    NodeId = _form.NodeId,
                    ClusterId = ClusterId,
                    Host = _form.Host,
                    OpcUaPort = _form.OpcUaPort,
                    DashboardPort = _form.DashboardPort,
                    ApplicationUri = _form.ApplicationUri,
                    ServiceLevelBase = _form.ServiceLevelBase,
                    Enabled = _form.Enabled,
                    DriverConfigOverridesJson = string.IsNullOrWhiteSpace(_form.DriverConfigOverridesJson) ? null : _form.DriverConfigOverridesJson,
                    CreatedAt = DateTime.UtcNow,
                    CreatedBy = auth.User.Identity?.Name ?? "(anonymous)",
                });
            }
            else
            {
                var entity = await db.ClusterNodes.FirstOrDefaultAsync(
                    n => n.ClusterId == ClusterId && n.NodeId == NodeId);
                if (entity is null) { _error = "Row no longer exists."; return; }
                entity.Host = _form.Host;
                entity.OpcUaPort = _form.OpcUaPort;
                entity.DashboardPort = _form.DashboardPort;
                entity.ApplicationUri = _form.ApplicationUri;
                entity.ServiceLevelBase = _form.ServiceLevelBase;
                entity.Enabled = _form.Enabled;
                entity.DriverConfigOverridesJson = string.IsNullOrWhiteSpace(_form.DriverConfigOverridesJson) ? null : _form.DriverConfigOverridesJson;
            }
            await db.SaveChangesAsync();
            Nav.NavigateTo($"/clusters/{ClusterId}");
        }
        catch (Exception ex)
        {
            _error = ex.Message;
        }
        finally
        {
            _busy = false;
        }
    }

    private async Task DeleteAsync()
    {
        if (IsNew) return;
        _busy = true;
        _error = null;
        try
        {
            await using var db = await DbFactory.CreateDbContextAsync();
            var entity = await db.ClusterNodes.FirstOrDefaultAsync(
                n => n.ClusterId == ClusterId && n.NodeId == NodeId);
            if (entity is null) { Nav.NavigateTo($"/clusters/{ClusterId}"); return; }
            db.ClusterNodes.Remove(entity);
            await db.SaveChangesAsync();
            Nav.NavigateTo($"/clusters/{ClusterId}");
        }
        catch (Exception ex)
        {
            _error = ex.Message;
        }
        finally
        {
            _busy = false;
        }
    }

    private sealed class FormModel
    {
        [Required, RegularExpression("^[A-Za-z0-9_-]+$")]
        public string NodeId { get; set; } = "";
        [Required] public string Host { get; set; } = "";
        [Range(1, 65535)] public int OpcUaPort { get; set; } = 4840;
        [Range(1, 65535)] public int DashboardPort { get; set; } = 8081;
        [Required, RegularExpression("^urn:[A-Za-z0-9_:./-]+$")]
        public string ApplicationUri { get; set; } = "";
        [Range(0, 255)] public byte ServiceLevelBase { get; set; } = 200;
        public bool Enabled { get; set; } = true;
        public string? DriverConfigOverridesJson { get; set; }
    }
}
