using Microsoft.EntityFrameworkCore;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.Configuration.Validation;
///
/// Loads the driver-instance identities the cross-instance credential rule (lmxopcua#835,
/// ) matches against — the ONE query the AdminUI save
/// path, live browse and Test Connect share (the deploy gate reads the same rows from its draft snapshot).
///
public static class DriverInstanceIdentities
{
/// Every stored driver instance of , fleet-wide.
/// The config DB.
/// The driver type (compared case-insensitively, client-side).
/// Cancellation.
/// The identities.
public static async Task> LoadAsync(
OtOpcUaConfigDbContext db, string driverType, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(db);
var rows = await db.DriverInstances.AsNoTracking()
.Select(d => new { d.DriverInstanceId, d.Name, d.DriverType })
.ToListAsync(ct).ConfigureAwait(false);
return rows
.Where(r => string.Equals(r.DriverType, driverType, StringComparison.OrdinalIgnoreCase))
.Select(r => new DriverInstanceIdentity(r.DriverInstanceId, r.Name, r.DriverType))
.ToList();
}
///
/// The cross-instance problems of — reading the ConfigDb only when the config carries
/// an in-scope secret: reference at all.
///
/// The config DB.
/// The owning driver's type.
/// The config about to be stored / probed / browsed.
/// The owning driver; null when it is being created.
/// Cancellation.
/// The problems.
public static async Task> DescribeOtherInstanceProblemsAsync(
OtOpcUaConfigDbContext db, string? driverType, string? configJson, string? ownDriverInstanceId, CancellationToken ct)
{
if (driverType is null || !DriverCredentialPolicy.HasInstanceSegments(driverType, configJson)) return [];
var instances = await LoadAsync(db, driverType, ct).ConfigureAwait(false);
return DriverCredentialPolicy.DescribeOtherInstanceProblems(driverType, configJson, ownDriverInstanceId, instances);
}
}