package runtime

import (
	"errors"
	"testing"
)

// TestOrdersAcceptStatus pins down the Phase 25 pre-check that
// gates the user-games command/order handlers against the runtime
// record. The decision must distinguish a turn cutoff (engine is
// producing) from a paused game so the UI can surface the right
// banner; all other non-running runtime statuses collapse into
// `ErrGamePaused`.
func TestOrdersAcceptStatus(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name string
		rec  RuntimeRecord
		want error
	}{
		{
			name: "running and not paused accepts orders",
			rec:  RuntimeRecord{Status: RuntimeStatusRunning, Paused: false},
			want: nil,
		},
		{
			name: "running but paused returns game paused",
			rec:  RuntimeRecord{Status: RuntimeStatusRunning, Paused: true},
			want: ErrGamePaused,
		},
		{
			name: "generation in progress returns turn already closed",
			rec:  RuntimeRecord{Status: RuntimeStatusGenerationInProgress},
			want: ErrTurnAlreadyClosed,
		},
		{
			name: "generation failed returns game paused",
			rec:  RuntimeRecord{Status: RuntimeStatusGenerationFailed},
			want: ErrGamePaused,
		},
		{
			name: "engine unreachable returns game paused",
			rec:  RuntimeRecord{Status: RuntimeStatusEngineUnreachable},
			want: ErrGamePaused,
		},
		{
			name: "stopped returns game paused",
			rec:  RuntimeRecord{Status: RuntimeStatusStopped},
			want: ErrGamePaused,
		},
		{
			name: "finished returns game paused",
			rec:  RuntimeRecord{Status: RuntimeStatusFinished},
			want: ErrGamePaused,
		},
		{
			name: "removed returns game paused",
			rec:  RuntimeRecord{Status: RuntimeStatusRemoved},
			want: ErrGamePaused,
		},
		{
			name: "starting returns game paused",
			rec:  RuntimeRecord{Status: RuntimeStatusStarting},
			want: ErrGamePaused,
		},
		{
			name: "paused takes precedence over generation in progress",
			rec:  RuntimeRecord{Status: RuntimeStatusGenerationInProgress, Paused: true},
			want: ErrGamePaused,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()
			got := OrdersAcceptStatus(tt.rec)
			if !errors.Is(got, tt.want) {
				t.Errorf("OrdersAcceptStatus = %v, want %v", got, tt.want)
			}
		})
	}
}
