using System.Security.Claims;
using System.Text.Json;
using Akka.Actor;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.Logging;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
using ZB.MOM.WW.ScadaBridge.ManagementService;
using ZB.MOM.WW.ScadaBridge.Security;
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Services;
///
/// Default implementation — a thin
/// read-only facade that dispatches the
/// to the central ManagementActor through the in-process
/// (the same Ask seam the HTTP /management
/// endpoint uses). The actor walks the full inheritance chain and returns the
/// effective member set + staleness; none of that is re-implemented here. Mirrors
/// 's read path.
///
public sealed class TemplateInheritanceQueryService : ITemplateInheritanceQueryService
{
///
/// camelCase + case-insensitive, matching ManagementActor.SerializeResult's
/// options. is produced with those settings,
/// so the deserializer must mirror them to bind every property.
///
private static readonly JsonSerializerOptions ResultDeserializerOptions = new()
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
private static readonly TimeSpan AskTimeout = TimeSpan.FromSeconds(30);
private readonly ManagementActorHolder _holder;
private readonly AuthenticationStateProvider _auth;
private readonly ILogger _logger;
///
/// Initializes a new instance of the .
///
/// Holder for the central ManagementActor reference.
/// Authentication state provider used to project the current principal.
/// Logger instance.
public TemplateInheritanceQueryService(
ManagementActorHolder holder,
AuthenticationStateProvider auth,
ILogger logger)
{
_holder = holder ?? throw new ArgumentNullException(nameof(holder));
_auth = auth ?? throw new ArgumentNullException(nameof(auth));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
///
public async Task ResolveAsync(
int templateId, CancellationToken cancellationToken = default)
{
var response = await SendAsync(new GetResolvedTemplateMembersCommand(templateId), cancellationToken);
if (response is ManagementSuccess success)
{
return JsonSerializer.Deserialize(
success.JsonData, ResultDeserializerOptions);
}
// Read path: log + return null so the editor falls back to its stored-row view.
_logger.LogWarning(
"GetResolvedTemplateMembers failed for template {TemplateId}: {Response}",
templateId, DescribeFailure(response));
return null;
}
///
/// Wraps in a for the
/// current principal and Asks the ManagementActor. Transport faults (timeout,
/// actor not yet started) become a synthetic so the
/// caller handles one response shape.
///
private async Task