#nullable enable

namespace MBN_STOCK_WEBVIEW.Core.Playout;

/// <summary>
/// Immutable observation of every native-runtime condition that must be idle
/// before an operator-data mutation can start.
/// </summary>
public sealed record OperatorMutationGateSnapshot(
    bool IsFamilyWriteQuarantined,
    bool IsGlobalWriteQuarantined,
    bool IsLifetimeCancellationRequested,
    bool IsPlayoutCommandInFlight,
    bool IsPlayoutShutdownStarted,
    bool IsBrowserCorrelationQuarantined,
    bool IsEngineAvailable,
    bool IsWorkflowAvailable,
    bool IsCommandAvailable,
    bool IsPlayCompletionPending,
    string? PreparedSceneName,
    string? OnAirSceneName,
    string? PreparedCutCode,
    string? OnAirCutCode,
    bool IsRefreshActive,
    bool IsRefreshTaskRunning);

/// <summary>
/// Pure fail-closed evaluator for the operator-data mutation boundary.
/// </summary>
public static class OperatorMutationGate
{
    public static bool CanStart(OperatorMutationGateSnapshot? snapshot) =>
        snapshot is not null &&
        !snapshot.IsFamilyWriteQuarantined &&
        !snapshot.IsGlobalWriteQuarantined &&
        !snapshot.IsLifetimeCancellationRequested &&
        !snapshot.IsPlayoutCommandInFlight &&
        !snapshot.IsPlayoutShutdownStarted &&
        !snapshot.IsBrowserCorrelationQuarantined &&
        snapshot.IsEngineAvailable &&
        snapshot.IsWorkflowAvailable &&
        snapshot.IsCommandAvailable &&
        !snapshot.IsPlayCompletionPending &&
        string.IsNullOrWhiteSpace(snapshot.PreparedSceneName) &&
        string.IsNullOrWhiteSpace(snapshot.OnAirSceneName) &&
        string.IsNullOrWhiteSpace(snapshot.PreparedCutCode) &&
        string.IsNullOrWhiteSpace(snapshot.OnAirCutCode) &&
        !snapshot.IsRefreshActive &&
        !snapshot.IsRefreshTaskRunning;
}
