using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;

namespace ZB.MOM.WW.ScadaBridge.DeploymentManager;

/// <summary>
/// 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.
/// </summary>
public static class StateTransitionValidator
{
    /// <summary>Returns true when a deploy operation is allowed from the given state.</summary>
    /// <param name="currentState">The current instance state.</param>
    /// <returns><see langword="true"/> if deploy is permitted; otherwise <see langword="false"/>.</returns>
    public static bool CanDeploy(InstanceState currentState) =>
        currentState is InstanceState.NotDeployed or InstanceState.Enabled or InstanceState.Disabled;

    /// <summary>Returns true when a disable operation is allowed from the given state.</summary>
    /// <param name="currentState">The current instance state.</param>
    /// <returns><see langword="true"/> if disable is permitted; otherwise <see langword="false"/>.</returns>
    public static bool CanDisable(InstanceState currentState) =>
        currentState == InstanceState.Enabled;

    /// <summary>Returns true when an enable operation is allowed from the given state.</summary>
    /// <param name="currentState">The current instance state.</param>
    /// <returns><see langword="true"/> if enable is permitted; otherwise <see langword="false"/>.</returns>
    public static bool CanEnable(InstanceState currentState) =>
        currentState == InstanceState.Disabled;

    /// <summary>Returns true when a delete operation is allowed from the given state.</summary>
    /// <param name="currentState">The current instance state.</param>
    /// <returns><see langword="true"/> if delete is permitted; otherwise <see langword="false"/>.</returns>
    /// <remarks>
    /// Delete is allowed from <see cref="InstanceState.NotDeployed"/> by design: an
    /// undeployed instance would otherwise linger as an unremovable orphan record.
    /// Delete from <c>NotDeployed</c> 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;
    /// the deliberate behaviour was introduced in commit 1d5465f3.
    /// </remarks>
    public static bool CanDelete(InstanceState currentState) =>
        currentState is InstanceState.NotDeployed or InstanceState.Enabled or InstanceState.Disabled;

    /// <summary>
    /// Returns a human-readable error message if the transition is invalid, or null if valid.
    /// </summary>
    /// <param name="currentState">The current instance state.</param>
    /// <param name="operation">The operation name to validate (e.g. "deploy", "disable", "enable", "delete").</param>
    /// <returns>An error message string if the transition is invalid; null if it is permitted.</returns>
    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}'.";
    }
}
