using System.CommandLine;
using System.CommandLine.Parsing;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
namespace ZB.MOM.WW.ScadaBridge.CLI.Commands;
///
/// Builds the cached-call command group with sub-commands for retrying and
/// discarding parked cached-call operations. Both sub-commands relay to the existing
/// Deployer-gated /
/// management commands via the central
/// SiteCallAuditActor → site relay.
///
public static class CachedCallCommands
{
// Options are static so the parsed values can be read back from both SetAction
// and the internal BuildRetryCommand / BuildDiscardCommand helpers (used by tests).
// A single shared instance per option is safe: retry and discard are sibling
// sub-commands and cannot be invoked simultaneously.
private static readonly Option SiteIdOption =
new("--site-id") { Description = "Site identifier of the parked operation", Required = true };
private static readonly Option MessageIdOption =
new("--tracked-operation-id") { Description = "Tracked operation ID (MessageId) of the parked cached call", Required = true };
///
/// Builds the cached-call command group.
///
/// Global --url option for the management API endpoint.
/// Global --format option for output format.
/// Global --username option for authentication.
/// Global --password option for authentication.
/// The configured cached-call command with retry and discard sub-commands.
public static Command Build(Option urlOption, Option formatOption, Option usernameOption, Option passwordOption)
{
var command = new Command("cached-call") { Description = "Manage parked cached-call operations (ExternalSystem.CachedCall / Database.CachedWrite)" };
command.Add(BuildRetry(urlOption, formatOption, usernameOption, passwordOption));
command.Add(BuildDiscard(urlOption, formatOption, usernameOption, passwordOption));
return command;
}
private static Command BuildRetry(Option urlOption, Option formatOption, Option usernameOption, Option passwordOption)
{
var cmd = new Command("retry") { Description = "Retry a parked cached-call operation" };
cmd.Add(SiteIdOption);
cmd.Add(MessageIdOption);
cmd.SetAction(async (ParseResult result) =>
{
return await CommandHelpers.ExecuteCommandAsync(
result, urlOption, formatOption, usernameOption, passwordOption,
BuildRetryCommand(result));
});
return cmd;
}
private static Command BuildDiscard(Option urlOption, Option formatOption, Option usernameOption, Option passwordOption)
{
var cmd = new Command("discard") { Description = "Discard a parked cached-call operation" };
cmd.Add(SiteIdOption);
cmd.Add(MessageIdOption);
cmd.SetAction(async (ParseResult result) =>
{
return await CommandHelpers.ExecuteCommandAsync(
result, urlOption, formatOption, usernameOption, passwordOption,
BuildDiscardCommand(result));
});
return cmd;
}
///
/// Builds a from a parsed cached-call retry
/// invocation. Exposed internally so command-construction tests can assert field mapping
/// without triggering the HTTP call.
///
/// The parsed command-line result from the cached-call retry invocation.
/// A populated from the parsed result.
internal static RetryParkedMessageCommand BuildRetryCommand(ParseResult result)
{
var siteId = result.GetValue(SiteIdOption)!;
var messageId = result.GetValue(MessageIdOption)!;
return new RetryParkedMessageCommand(siteId, messageId);
}
///
/// Builds a from a parsed cached-call discard
/// invocation. Exposed internally so command-construction tests can assert field mapping
/// without triggering the HTTP call.
///
/// The parsed command-line result from the cached-call discard invocation.
/// A populated from the parsed result.
internal static DiscardParkedMessageCommand BuildDiscardCommand(ParseResult result)
{
var siteId = result.GetValue(SiteIdOption)!;
var messageId = result.GetValue(MessageIdOption)!;
return new DiscardParkedMessageCommand(siteId, messageId);
}
}