package cassandra

import (
	"testing"

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

func TestOrder_ToGocqlX(t *testing.T) {
	tests := []struct {
		name     string
		order    Order
		expected string
	}{
		{
			name:     "ASC order",
			order:    ASC,
			expected: "ASC",
		},
		{
			name:     "DESC order",
			order:    DESC,
			expected: "DESC",
		},
		{
			name:     "zero value (defaults to ASC)",
			order:    Order(0),
			expected: "ASC",
		},
		{
			name:     "invalid order value (defaults to ASC)",
			order:    Order(99),
			expected: "ASC",
		},
		{
			name:     "negative order value (defaults to ASC)",
			order:    Order(-1),
			expected: "ASC",
		},
	}

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

func TestOrder_Constants(t *testing.T) {
	tests := []struct {
		name     string
		constant Order
		expected int
	}{
		{
			name:     "ASC constant",
			constant: ASC,
			expected: 0,
		},
		{
			name:     "DESC constant",
			constant: DESC,
			expected: 1,
		},
	}

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

func TestOrder_StringConversion(t *testing.T) {
	tests := []struct {
		name     string
		order    Order
		expected string
	}{
		{
			name:     "ASC to string",
			order:    ASC,
			expected: "ASC",
		},
		{
			name:     "DESC to string",
			order:    DESC,
			expected: "DESC",
		},
	}

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

func TestOrder_Comparison(t *testing.T) {
	t.Run("ASC should equal 0", func(t *testing.T) {
		assert.Equal(t, Order(0), ASC)
	})

	t.Run("DESC should equal 1", func(t *testing.T) {
		assert.Equal(t, Order(1), DESC)
	})

	t.Run("ASC should not equal DESC", func(t *testing.T) {
		assert.NotEqual(t, ASC, DESC)
	})
}

func TestOrder_EdgeCases(t *testing.T) {
	tests := []struct {
		name     string
		order    Order
		expected string
	}{
		{
			name:     "maximum int value",
			order:    Order(^int(0)),
			expected: "ASC", // 不是 DESC，所以返回 ASC
		},
		{
			name:     "minimum int value",
			order:    Order(-^int(0) - 1),
			expected: "ASC",
		},
	}

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

