package domain

import (
	"testing"

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

// TestStrPtr 测试 StrPtr 函数
func TestStrPtr(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected *string
	}{
		{
			name:     "empty string",
			input:    "",
			expected: nil,
		},
		{
			name:     "non-empty string",
			input:    "test@example.com",
			expected: strPtr("test@example.com"),
		},
		{
			name:     "whitespace string",
			input:    "   ",
			expected: strPtr("   "),
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := StrPtr(tt.input)
			if tt.expected == nil {
				assert.Nil(t, got)
			} else {
				assert.NotNil(t, got)
				assert.Equal(t, *tt.expected, *got)
			}
		})
	}
}

// TestDerefStr 测试 DerefStr 函数
func TestDerefStr(t *testing.T) {
	tests := []struct {
		name     string
		input    *string
		expected string
	}{
		{
			name:     "nil pointer",
			input:    nil,
			expected: "",
		},
		{
			name:     "non-nil pointer",
			input:    strPtr("test@example.com"),
			expected: "test@example.com",
		},
		{
			name:     "empty string pointer",
			input:    strPtr(""),
			expected: "",
		},
	}

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

// strPtr 辅助函数，返回字符串指针
func strPtr(s string) *string {
	return &s
}

// TestGender_Constants 测试性别常量
func TestGender_Constants(t *testing.T) {
	assert.Equal(t, Gender(0), GenderUnknown)
	assert.Equal(t, Gender(1), GenderMale)
	assert.Equal(t, Gender(2), GenderFemale)
}

// TestUserStatus_Constants 测试用户状态常量
func TestUserStatus_Constants(t *testing.T) {
	assert.Equal(t, UserStatus(0), UserStatusInactive)
	assert.Equal(t, UserStatus(1), UserStatusActive)
	assert.Equal(t, UserStatus(2), UserStatusLocked)
	assert.Equal(t, UserStatus(3), UserStatusDisabled)
}

// TestUser_TableName 测试用户表名
func TestUser_TableName(t *testing.T) {
	user := User{}
	assert.Equal(t, "users", user.TableName())
}

// TestUser_DefaultValues 测试用户默认值
func TestUser_DefaultValues(t *testing.T) {
	user := User{}
	assert.Equal(t, GenderUnknown, user.Gender)
	assert.Equal(t, UserStatusInactive, user.Status)
	assert.False(t, user.TOTPEnabled)
}

// TestStrPtr_DerefStr_RoundTrip 测试往返
func TestStrPtr_DerefStr_RoundTrip(t *testing.T) {
	original := "test@example.com"
	ptr := StrPtr(original)
	got := DerefStr(ptr)
	assert.Equal(t, original, got)

	// 注意：StrPtr("") 返回 nil，不是指向空字符串的指针
	// 这是设计决定的，空字符串表示该字段未设置
}

// TestStrPtr_NilDeref 测试 nil 解引用
func TestStrPtr_NilDeref(t *testing.T) {
	// 空字符串返回 nil
	assert.Nil(t, StrPtr(""))
	// nil 解引用返回空字符串
	assert.Equal(t, "", DerefStr(nil))
}
