package utils

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

func NormalizeEmail(email string) (string, error) {
	if email == "" {
		return "", errors.New("email empty")
	}

	lower := strings.ToLower(email)
	lower = strings.TrimSpace(lower)

	parsed, err := mail.ParseAddress(lower)
	if err != nil {
		return "", err
	}

	return parsed.Address, nil
}

func IsValidEmail(email string) bool {
	_, err := mail.ParseAddress(email)
	return err == nil
}
