package cassandra

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestDefaultSAIIndexOptions(t *testing.T) {
	opts := DefaultSAIIndexOptions()
	assert.NotNil(t, opts)
	assert.Equal(t, SAIIndexTypeStandard, opts.IndexType)
	assert.False(t, opts.IsAsync)
	assert.True(t, opts.CaseSensitive)
}

func TestCreateSAIIndex_Validation(t *testing.T) {
	tests := []struct {
		name      string
		keyspace  string
		table     string
		column    string
		indexName string
		opts      *SAIIndexOptions
		wantErr   bool
		errMsg    string
	}{
		{
			name:      "missing keyspace",
			keyspace:  "",
			table:     "test_table",
			column:    "test_column",
			indexName: "test_idx",
			opts:      nil,
			wantErr:   true,
			errMsg:    "keyspace is required",
		},
		{
			name:      "missing table",
			keyspace:  "test_keyspace",
			table:     "",
			column:    "test_column",
			indexName: "test_idx",
			opts:      nil,
			wantErr:   true,
			errMsg:    "table is required",
		},
		{
			name:      "missing column",
			keyspace:  "test_keyspace",
			table:     "test_table",
			column:    "",
			indexName: "test_idx",
			opts:      nil,
			wantErr:   true,
			errMsg:    "column is required",
		},
		{
			name:      "valid parameters with default options",
			keyspace:  "test_keyspace",
			table:     "test_table",
			column:    "test_column",
			indexName: "test_idx",
			opts:      nil,
			wantErr:   false,
		},
		{
			name:      "valid parameters with custom options",
			keyspace:  "test_keyspace",
			table:     "test_table",
			column:    "test_column",
			indexName: "test_idx",
			opts: &SAIIndexOptions{
				IndexType:     SAIIndexTypeFullText,
				IsAsync:       true,
				CaseSensitive: false,
			},
			wantErr: false,
		},
		{
			name:      "auto-generate index name",
			keyspace:  "test_keyspace",
			table:     "test_table",
			column:    "test_column",
			indexName: "",
			opts:      nil,
			wantErr:   false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// 注意：這需要一個有效的 DB 實例和 SAI 支援
			// 在實際測試中，需要使用 mock 或 testcontainers
			_ = tt
		})
	}
}

func TestDropSAIIndex_Validation(t *testing.T) {
	tests := []struct {
		name      string
		keyspace  string
		indexName string
		wantErr   bool
		errMsg    string
	}{
		{
			name:      "missing keyspace",
			keyspace:  "",
			indexName: "test_idx",
			wantErr:   true,
			errMsg:    "keyspace is required",
		},
		{
			name:      "missing index name",
			keyspace:  "test_keyspace",
			indexName: "",
			wantErr:   true,
			errMsg:    "index name is required",
		},
		{
			name:      "valid parameters",
			keyspace:  "test_keyspace",
			indexName: "test_idx",
			wantErr:   false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// 注意：這需要一個有效的 DB 實例
			// 在實際測試中，需要使用 mock 或 testcontainers
			_ = tt
		})
	}
}

func TestListSAIIndexes_Validation(t *testing.T) {
	tests := []struct {
		name     string
		keyspace string
		table    string
		wantErr  bool
		errMsg   string
	}{
		{
			name:     "missing keyspace",
			keyspace: "",
			table:    "test_table",
			wantErr:  true,
			errMsg:   "keyspace is required",
		},
		{
			name:     "missing table",
			keyspace: "test_keyspace",
			table:    "",
			wantErr:  true,
			errMsg:   "table is required",
		},
		{
			name:     "valid parameters",
			keyspace: "test_keyspace",
			table:    "test_table",
			wantErr:  false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// 注意：這需要一個有效的 DB 實例
			// 在實際測試中，需要使用 mock 或 testcontainers
			_ = tt
		})
	}
}

func TestCheckSAIIndexExists_Validation(t *testing.T) {
	tests := []struct {
		name      string
		keyspace  string
		indexName string
		wantErr   bool
		errMsg    string
	}{
		{
			name:      "missing keyspace",
			keyspace:  "",
			indexName: "test_idx",
			wantErr:   true,
			errMsg:    "keyspace is required",
		},
		{
			name:      "missing index name",
			keyspace:  "test_keyspace",
			indexName: "",
			wantErr:   true,
			errMsg:    "index name is required",
		},
		{
			name:      "valid parameters",
			keyspace:  "test_keyspace",
			indexName: "test_idx",
			wantErr:   false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// 注意：這需要一個有效的 DB 實例
			// 在實際測試中，需要使用 mock 或 testcontainers
			_ = tt
		})
	}
}

func TestSAIIndexType_Constants(t *testing.T) {
	tests := []struct {
		name      string
		indexType SAIIndexType
		expected  string
	}{
		{
			name:      "standard index type",
			indexType: SAIIndexTypeStandard,
			expected:  "STANDARD",
		},
		{
			name:      "collection index type",
			indexType: SAIIndexTypeCollection,
			expected:  "COLLECTION",
		},
		{
			name:      "full text index type",
			indexType: SAIIndexTypeFullText,
			expected:  "FULL_TEXT",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			assert.Equal(t, tt.expected, string(tt.indexType))
		})
	}
}

func TestCreateSAIIndex_NotSupported(t *testing.T) {
	t.Run("should return error when SAI not supported", func(t *testing.T) {
		// 注意：這需要一個不支援 SAI 的 DB 實例
		// 在實際測試中，需要使用 mock 或 testcontainers
	})
}

func TestCreateSAIIndex_IndexNameGeneration(t *testing.T) {
	t.Run("should generate index name when not provided", func(t *testing.T) {
		// 測試自動生成索引名稱的邏輯
		// 格式應該是: {table}_{column}_sai_idx
		table := "users"
		column := "email"
		expected := "users_email_sai_idx"

		// 這裡只是測試命名邏輯，實際建立需要 DB 實例
		generated := fmt.Sprintf("%s_%s_sai_idx", table, column)
		assert.Equal(t, expected, generated)
	})
}
