using Microsoft.EntityFrameworkCore;
using ZB.MOM.WW.OtOpcUa.Configuration;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Clients;
///
/// Helpers for the per-node diagnostics page (A-F4 / A-F5). /fleet/diagnostics was reachable only
/// by URL; /fleet now links each driver node's row to it. The page also showed the driver's
/// string instance id where the operator expects its authored name — the diagnostics wire carries only
/// the id (DriverInstanceDiagnostics.Name), so the AdminUI joins the name from the ConfigDb.
///
public static class FleetDiagnosticsLinks
{
/// The diagnostics page route.
public const string PagePath = "/fleet/diagnostics";
/// The diagnostics URL for one node.
/// The node id as /fleet shows it (host:port).
/// A relative URL with the node escaped.
public static string For(string nodeId) => PagePath + "?node=" + Uri.EscapeDataString(nodeId);
/// Loads the authored display names of the given driver instance ids.
/// The config DB context.
/// The string driver instance ids from the snapshot.
/// Cancellation token.
/// Id → authored name (ordinal-ignore-case keys); ids with no configured row are absent.
public static async Task> LoadDisplayNamesAsync(
OtOpcUaConfigDbContext db, IReadOnlyCollection driverInstanceIds, CancellationToken ct = default)
{
if (driverInstanceIds.Count == 0) return new Dictionary(StringComparer.OrdinalIgnoreCase);
var ids = driverInstanceIds.Distinct(StringComparer.OrdinalIgnoreCase).ToList();
var rows = await db.DriverInstances.AsNoTracking()
.Where(d => ids.Contains(d.DriverInstanceId))
.Select(d => new { d.DriverInstanceId, d.Name })
.ToListAsync(ct);
var map = new Dictionary(StringComparer.OrdinalIgnoreCase);
foreach (var r in rows)
{
if (!string.IsNullOrWhiteSpace(r.Name)) map[r.DriverInstanceId] = r.Name;
}
return map;
}
}