package customer

import (
	"regexp"

	"github.com/asaskevich/govalidator"
)

// ValidationService defines validation methods for customer operations
type ValidationService interface {
	IsValidUsername(username string) bool
}

// validationService implements the ValidationService interface
type validationService struct {
	usernameRegex *regexp.Regexp
}

// NewValidationService creates a new validation service
func NewValidationService() ValidationService {
	// Compile the username regex pattern
	usernameRegex := regexp.MustCompile(`^[a-zA-Z0-9](?:[a-zA-Z0-9._]*[a-zA-Z0-9])?$`)

	// Register custom validator with govalidator
	govalidator.CustomTypeTagMap.Set("username", govalidator.CustomTypeValidator(func(i interface{}, o interface{}) bool {
		username, ok := i.(string)
		if !ok {
			return false
		}
		return usernameRegex.MatchString(username)
	}))

	return &validationService{
		usernameRegex: usernameRegex,
	}
}

// IsValidUsername checks if a username matches the required pattern
func (v *validationService) IsValidUsername(username string) bool {
	return v.usernameRegex.MatchString(username)
}
