package unit

import (
	"testing"

	"github.com/stretchr/testify/assert"

	"github.com/break/junhong_cmp_fiber/pkg/constants"
)

// TestIsRoleTypeMatchUserType 测试角色类型与用户类型匹配规则
func TestIsRoleTypeMatchUserType(t *testing.T) {
	tests := []struct {
		name     string
		roleType int
		userType int
		expected bool
	}{
		{
			name:     "超级管理员不需要角色",
			roleType: constants.RoleTypePlatform,
			userType: constants.UserTypeSuperAdmin,
			expected: false,
		},
		{
			name:     "平台用户匹配平台角色",
			roleType: constants.RoleTypePlatform,
			userType: constants.UserTypePlatform,
			expected: true,
		},
		{
			name:     "平台用户不匹配客户角色",
			roleType: constants.RoleTypeCustomer,
			userType: constants.UserTypePlatform,
			expected: false,
		},
		{
			name:     "代理账号匹配客户角色",
			roleType: constants.RoleTypeCustomer,
			userType: constants.UserTypeAgent,
			expected: true,
		},
		{
			name:     "代理账号不匹配平台角色",
			roleType: constants.RoleTypePlatform,
			userType: constants.UserTypeAgent,
			expected: false,
		},
		{
			name:     "企业账号匹配客户角色",
			roleType: constants.RoleTypeCustomer,
			userType: constants.UserTypeEnterprise,
			expected: true,
		},
		{
			name:     "企业账号不匹配平台角色",
			roleType: constants.RoleTypePlatform,
			userType: constants.UserTypeEnterprise,
			expected: false,
		},
	}

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

// TestGetMaxRolesForUserType 测试用户类型的最大角色数量限制
func TestGetMaxRolesForUserType(t *testing.T) {
	tests := []struct {
		name     string
		userType int
		expected int
	}{
		{
			name:     "超级管理员不需要角色",
			userType: constants.UserTypeSuperAdmin,
			expected: 0,
		},
		{
			name:     "平台用户无角色数量限制",
			userType: constants.UserTypePlatform,
			expected: -1, // -1 表示无限制
		},
		{
			name:     "代理账号最多一个角色",
			userType: constants.UserTypeAgent,
			expected: 1,
		},
		{
			name:     "企业账号最多一个角色",
			userType: constants.UserTypeEnterprise,
			expected: 1,
		},
		{
			name:     "未知用户类型不允许角色",
			userType: 999,
			expected: 0,
		},
	}

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