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.
/// if deploy is permitted; otherwise .
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.
/// if disable is permitted; otherwise .
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.
/// if enable is permitted; otherwise .
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.
/// if delete is permitted; otherwise .
///
/// Delete is allowed from by design: an
/// undeployed instance would otherwise linger as an unremovable orphan record.
/// Delete from NotDeployed is a central-side record cleanup (no live site
/// config to tear down). This matches the state-transition matrix in
/// Component-DeploymentManager.md ("Delete from Not deployed = Yes") — reconciled
/// in M2.17 (#31); the deliberate behaviour was introduced in commit 1d5465f3.
///
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").
/// An error message string if the transition is invalid; null if it is permitted.
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}'.";
}
}