package validator

import (
	"testing"

	"github.com/break/junhong_cmp_fiber/pkg/constants"
	"github.com/stretchr/testify/assert"
)

func TestValidateICCID(t *testing.T) {
	tests := []struct {
		name        string
		iccid       string
		carrierType string
		wantValid   bool
		wantMessage string
	}{
		// 空值测试
		{
			name:        "空ICCID应该返回错误",
			iccid:       "",
			carrierType: constants.CarrierCodeCMCC,
			wantValid:   false,
			wantMessage: "ICCID 不能为空",
		},

		// 电信 ICCID 测试（19位）
		{
			name:        "电信有效ICCID-19位数字",
			iccid:       "8986031234567890123",
			carrierType: constants.CarrierCodeCTCC,
			wantValid:   true,
			wantMessage: "",
		},
		{
			name:        "电信ICCID-20位应该失败",
			iccid:       "89860312345678901234",
			carrierType: constants.CarrierCodeCTCC,
			wantValid:   false,
			wantMessage: "电信 ICCID 必须为 19 位",
		},
		{
			name:        "电信ICCID-18位应该失败",
			iccid:       "898603123456789012",
			carrierType: constants.CarrierCodeCTCC,
			wantValid:   false,
			wantMessage: "电信 ICCID 必须为 19 位",
		},

		// 移动 ICCID 测试（20位）
		{
			name:        "移动有效ICCID-20位数字",
			iccid:       "89860012345678901234",
			carrierType: constants.CarrierCodeCMCC,
			wantValid:   true,
			wantMessage: "",
		},
		{
			name:        "移动有效ICCID-含字母",
			iccid:       "8986001234567890123A",
			carrierType: constants.CarrierCodeCMCC,
			wantValid:   true,
			wantMessage: "",
		},
		{
			name:        "移动ICCID-19位应该失败",
			iccid:       "8986001234567890123",
			carrierType: constants.CarrierCodeCMCC,
			wantValid:   false,
			wantMessage: "该运营商 ICCID 必须为 20 位",
		},

		// 联通 ICCID 测试（20位）
		{
			name:        "联通有效ICCID-20位数字",
			iccid:       "89860112345678901234",
			carrierType: constants.CarrierCodeCUCC,
			wantValid:   true,
			wantMessage: "",
		},
		{
			name:        "联通ICCID-21位应该失败",
			iccid:       "898601123456789012345",
			carrierType: constants.CarrierCodeCUCC,
			wantValid:   false,
			wantMessage: "该运营商 ICCID 必须为 20 位",
		},

		// 广电 ICCID 测试（20位）
		{
			name:        "广电有效ICCID-20位数字",
			iccid:       "89860412345678901234",
			carrierType: constants.CarrierCodeCBN,
			wantValid:   true,
			wantMessage: "",
		},

		// 特殊字符测试
		{
			name:        "ICCID包含特殊字符应该失败",
			iccid:       "8986001234567890123!",
			carrierType: constants.CarrierCodeCMCC,
			wantValid:   false,
			wantMessage: "ICCID 只能包含字母和数字",
		},
		{
			name:        "ICCID包含空格应该失败",
			iccid:       "8986001234567890123 ",
			carrierType: constants.CarrierCodeCMCC,
			wantValid:   false,
			wantMessage: "ICCID 只能包含字母和数字",
		},
		{
			name:        "ICCID包含中划线应该失败",
			iccid:       "8986001234-678901234",
			carrierType: constants.CarrierCodeCMCC,
			wantValid:   false,
			wantMessage: "ICCID 只能包含字母和数字",
		},

		// 大小写字母测试
		{
			name:        "ICCID包含小写字母有效",
			iccid:       "8986001234567890123a",
			carrierType: constants.CarrierCodeCMCC,
			wantValid:   true,
			wantMessage: "",
		},
		{
			name:        "ICCID包含大写字母有效",
			iccid:       "8986001234567890123A",
			carrierType: constants.CarrierCodeCMCC,
			wantValid:   true,
			wantMessage: "",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := ValidateICCID(tt.iccid, tt.carrierType)
			assert.Equal(t, tt.wantValid, result.Valid, "Valid 不匹配")
			assert.Equal(t, tt.wantMessage, result.Message, "Message 不匹配")
		})
	}
}

func TestValidateICCIDWithoutCarrier(t *testing.T) {
	tests := []struct {
		name        string
		iccid       string
		wantValid   bool
		wantMessage string
	}{
		// 空值测试
		{
			name:        "空ICCID应该返回错误",
			iccid:       "",
			wantValid:   false,
			wantMessage: "ICCID 不能为空",
		},

		// 有效长度测试（19位或20位）
		{
			name:        "19位ICCID有效",
			iccid:       "8986031234567890123",
			wantValid:   true,
			wantMessage: "",
		},
		{
			name:        "20位ICCID有效",
			iccid:       "89860012345678901234",
			wantValid:   true,
			wantMessage: "",
		},

		// 无效长度测试
		{
			name:        "18位ICCID无效",
			iccid:       "898603123456789012",
			wantValid:   false,
			wantMessage: "ICCID 长度必须为 19 位或 20 位",
		},
		{
			name:        "21位ICCID无效",
			iccid:       "898600123456789012345",
			wantValid:   false,
			wantMessage: "ICCID 长度必须为 19 位或 20 位",
		},

		// 特殊字符测试
		{
			name:        "包含特殊字符应该失败",
			iccid:       "8986001234567890123!",
			wantValid:   false,
			wantMessage: "ICCID 只能包含字母和数字",
		},

		// 字母数字混合测试
		{
			name:        "20位含字母有效",
			iccid:       "8986001234567890AB12",
			wantValid:   true,
			wantMessage: "",
		},
		{
			name:        "19位含字母有效",
			iccid:       "898603123456789AB12",
			wantValid:   true,
			wantMessage: "",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := ValidateICCIDWithoutCarrier(tt.iccid)
			assert.Equal(t, tt.wantValid, result.Valid, "Valid 不匹配")
			assert.Equal(t, tt.wantMessage, result.Message, "Message 不匹配")
		})
	}
}

// TestGetExpectedICCIDLength 测试获取期望的 ICCID 长度
func TestGetExpectedICCIDLength(t *testing.T) {
	tests := []struct {
		name           string
		carrierType    string
		expectedLength int
	}{
		{
			name:           "电信应该返回19",
			carrierType:    constants.CarrierCodeCTCC,
			expectedLength: 19,
		},
		{
			name:           "移动应该返回20",
			carrierType:    constants.CarrierCodeCMCC,
			expectedLength: 20,
		},
		{
			name:           "联通应该返回20",
			carrierType:    constants.CarrierCodeCUCC,
			expectedLength: 20,
		},
		{
			name:           "广电应该返回20",
			carrierType:    constants.CarrierCodeCBN,
			expectedLength: 20,
		},
		{
			name:           "未知运营商应该返回20",
			carrierType:    "UNKNOWN",
			expectedLength: 20,
		},
		{
			name:           "空运营商应该返回20",
			carrierType:    "",
			expectedLength: 20,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := getExpectedICCIDLength(tt.carrierType)
			assert.Equal(t, tt.expectedLength, result)
		})
	}
}
