from flask_mail import Message
from cpm_assessment import mail
import re

def send_email(to, subject, template):
    if isinstance(to, str):
        recipients = [to]
    else:
        recipients = to
    msg = Message(subject, recipients=recipients)
    msg.html = template
    mail.send(msg)

def consultant_prefix(consultant):
    name = (getattr(consultant, 'username', '') or '')
    letters = ''.join(ch for ch in name if ch.isalpha()).upper()
    first_four = (letters + 'XXXX')[:4]
    try:
        idx = int(getattr(consultant, 'id', 0)) % 26
    except Exception:
        idx = 0
    fifth = chr(ord('A') + idx)
    return (first_four + fifth)

def is_alphanumeric(s):
    return bool(re.fullmatch(r'[A-Za-z0-9]+', s or ''))
