package identity

import (
	"net/mail"
	"regexp"
	"strings"
)

var (
	usernamePattern = regexp.MustCompile(`^[a-zA-Z0-9_]{3,50}$`)
	phonePattern    = regexp.MustCompile(`^1[3-9]\d{9}$`)
	codePattern     = regexp.MustCompile(`^\d{6}$`)
)

func normalizeEmail(value string) string {
	return strings.ToLower(strings.TrimSpace(value))
}

func validateEmail(value string) bool {
	value = strings.TrimSpace(value)
	if value == "" {
		return false
	}
	_, err := mail.ParseAddress(value)
	return err == nil
}

func validateUsername(value string) bool {
	return usernamePattern.MatchString(strings.TrimSpace(value))
}

func validatePhone(value string) bool {
	return phonePattern.MatchString(strings.TrimSpace(value))
}

func validateCode(value string) bool {
	return codePattern.MatchString(strings.TrimSpace(value))
}

func validatePassword(value string) bool {
	return len(value) >= 8 && len(value) <= 128
}
