using MBN_STOCK_WEBVIEW.Core.Playout;

namespace MBN_STOCK_WEBVIEW.Core.Tests;

public sealed class OperatorMutationGateTests
{
    private static readonly OperatorMutationGateSnapshot Ready = new(
        IsFamilyWriteQuarantined: false,
        IsGlobalWriteQuarantined: false,
        IsLifetimeCancellationRequested: false,
        IsPlayoutCommandInFlight: false,
        IsPlayoutShutdownStarted: false,
        IsBrowserCorrelationQuarantined: false,
        IsEngineAvailable: true,
        IsWorkflowAvailable: true,
        IsCommandAvailable: true,
        IsPlayCompletionPending: false,
        PreparedSceneName: null,
        OnAirSceneName: null,
        PreparedCutCode: null,
        OnAirCutCode: null,
        IsRefreshActive: false,
        IsRefreshTaskRunning: false);

    public static TheoryData<string, OperatorMutationGateSnapshot> BlockingSnapshots =>
        new()
        {
            { "family write quarantine", Ready with { IsFamilyWriteQuarantined = true } },
            { "global write quarantine", Ready with { IsGlobalWriteQuarantined = true } },
            { "lifetime cancellation", Ready with { IsLifetimeCancellationRequested = true } },
            { "playout command in flight", Ready with { IsPlayoutCommandInFlight = true } },
            { "playout shutdown", Ready with { IsPlayoutShutdownStarted = true } },
            { "browser correlation quarantine", Ready with { IsBrowserCorrelationQuarantined = true } },
            { "missing engine", Ready with { IsEngineAvailable = false } },
            { "missing workflow", Ready with { IsWorkflowAvailable = false } },
            { "command unavailable", Ready with { IsCommandAvailable = false } },
            { "play completion pending", Ready with { IsPlayCompletionPending = true } },
            { "engine prepared scene", Ready with { PreparedSceneName = "prepared-scene" } },
            { "engine on-air scene", Ready with { OnAirSceneName = "on-air-scene" } },
            { "workflow prepared cut", Ready with { PreparedCutCode = "5001" } },
            { "workflow on-air cut", Ready with { OnAirCutCode = "5001" } },
            { "refresh active", Ready with { IsRefreshActive = true } },
            { "refresh task running", Ready with { IsRefreshTaskRunning = true } }
        };

    [Fact]
    public void CanStart_WhenEveryConditionIsSafe_ReturnsTrue()
    {
        Assert.True(OperatorMutationGate.CanStart(Ready));
    }

    [Theory]
    [MemberData(nameof(BlockingSnapshots))]
    public void CanStart_WhenOneConditionIsUnsafe_ReturnsFalse(
        string condition,
        OperatorMutationGateSnapshot snapshot)
    {
        Assert.NotEmpty(condition);
        Assert.False(OperatorMutationGate.CanStart(snapshot));
    }
}
