using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
namespace ZB.MOM.WW.ScadaBridge.DeploymentManager;
///
/// WP-4: State transition matrix for instance lifecycle.
///
/// State | Deploy | Disable | Enable | Delete
/// ----------|--------|---------|--------|-------
/// NotDeploy | OK | NO | NO | OK
/// Enabled | OK | OK | NO | OK
/// Disabled | OK* | NO | OK | OK
///
/// * Deploy on a Disabled instance also enables it.
/// Delete removes the instance record entirely; it is valid from any state.
///
public static class StateTransitionValidator
{
/// Returns true when a deploy operation is allowed from the given state.
/// The current instance state.
public static bool CanDeploy(InstanceState currentState) =>
currentState is InstanceState.NotDeployed or InstanceState.Enabled or InstanceState.Disabled;
/// Returns true when a disable operation is allowed from the given state.
/// The current instance state.
public static bool CanDisable(InstanceState currentState) =>
currentState == InstanceState.Enabled;
/// Returns true when an enable operation is allowed from the given state.
/// The current instance state.
public static bool CanEnable(InstanceState currentState) =>
currentState == InstanceState.Disabled;
/// Returns true when a delete operation is allowed from the given state.
/// The current instance state.
public static bool CanDelete(InstanceState currentState) =>
currentState is InstanceState.NotDeployed or InstanceState.Enabled or InstanceState.Disabled;
///
/// Returns a human-readable error message if the transition is invalid, or null if valid.
///
/// The current instance state.
/// The operation name to validate (e.g. "deploy", "disable", "enable", "delete").
public static string? ValidateTransition(InstanceState currentState, string operation)
{
var allowed = operation.ToLowerInvariant() switch
{
"deploy" => CanDeploy(currentState),
"disable" => CanDisable(currentState),
"enable" => CanEnable(currentState),
"delete" => CanDelete(currentState),
_ => false
};
if (allowed) return null;
return $"Operation '{operation}' is not allowed when instance is in state '{currentState}'.";
}
}