package spaces

import (
	"context"
	"testing"
)

// dummySpaces используется для имитации поведения Spaces
// Моки использовать не можем, так как получается циклический импорт
type dummySpaces struct {
	Spaces
	space *Space
}

func (t *dummySpaces) Get(_ context.Context, _ string) (*Space, error) { return t.space, nil }

func TestIsSpaceAvailable(t *testing.T) {
	tests := []struct {
		name    string
		space   *Space
		wantErr bool
	}{
		{
			"Space has nil StateInfo: available",
			&Space{ID: "space", OrgID: "org", Name: "test-space"},
			false,
		},
		{
			"Space state is StateReady: available",
			&Space{ID: "space", OrgID: "org", Name: "test-space", StateInfo: &StateInfo{State: StateReady}},
			false,
		},
		{
			"Space state is StatePreparing: not available",
			&Space{ID: "space", OrgID: "org", Name: "test-space", StateInfo: &StateInfo{State: StatePreparing}},
			true,
		},
		{
			"Space state is StateMigration: not available",
			&Space{ID: "space", OrgID: "org", Name: "test-space", StateInfo: &StateInfo{State: StateMigration}},
			true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			spaces := &dummySpaces{space: tt.space}
			if err := IsSpaceAvailable(context.Background(), spaces, "space"); (err != nil) != tt.wantErr {
				t.Errorf("IsSpaceAvailable() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}

func TestIsReadAvailable(t *testing.T) {
	tests := []struct {
		name    string
		space   *Space
		wantErr bool
	}{
		{
			"Space has nil StateInfo: available",
			&Space{ID: "space", OrgID: "org", Name: "test-space"},
			false,
		},
		{
			"Space state is StateReady: available",
			&Space{ID: "space", OrgID: "org", Name: "test-space", StateInfo: &StateInfo{State: StateReady}},
			false,
		},
		{
			"Space state is StatePreparing: not available",
			&Space{ID: "space", OrgID: "org", Name: "test-space", StateInfo: &StateInfo{State: StatePreparing}},
			true,
		},
		{
			"Space state is StateMigration: not available",
			&Space{ID: "space", OrgID: "org", Name: "test-space", StateInfo: &StateInfo{State: StateMigration}},
			false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			spaces := &dummySpaces{space: tt.space}
			if err := IsReadAvailable(context.Background(), spaces, "space"); (err != nil) != tt.wantErr {
				t.Errorf("IsReadAvailable() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}