# validators.py

from string import ascii_letters, digits

# ------------- EMAIL ------------- #

def has_at_symbol(email: str) -> bool:
    return "@" in email


def is_valid_username(email: str) -> bool:
    if email.count("@") != 1:
        return False

    username = email.split("@", 1)[0]
    if not username:
        return False

    allowed = set(ascii_letters + digits + ".")
    return all(ch in allowed for ch in username)


def find_domain(email: str) -> str:
    if "@" not in email:
        return ""
    return email.rsplit("@", 1)[1]


def is_valid_domain(email: str) -> bool:
    domain = find_domain(email)
    if not domain:
        return False

    if domain.count(".") != 1:
        return False

    left, right = domain.split(".", 1)

    if not (3 <= len(left) <= 10):
        return False
    if not (2 <= len(right) <= 5):
        return False

    if not (left.isalpha() and right.isalpha()):
        return False

    return True


def is_valid_email_address(email: str) -> bool:
    if email.count("@") != 1:
        return False

    return has_at_symbol(email) and is_valid_username(email) and is_valid_domain(email)


# ------------- PASSWORD ------------- #

def is_correct_length(password: str) -> bool:
    return 8 <= len(password) <= 64


def includes_uppercase(password: str) -> bool:
    for ch in password:
        if ch.isupper():
            return True
    return False


def includes_lowercase(password: str) -> bool:
    for ch in password:
        if ch.islower():
            return True
    return False


def includes_special(password: str) -> bool:
    for ch in password:
        if not ch.isalnum():
            return True
    return False


def includes_number(password: str) -> bool:
    for ch in password:
        if ch.isdigit():
            return True
    return False


def is_strong_basic_password(password: str) -> bool:
    """
    Lihtsustatud tugevuse kontroll konto LOOMISE jaoks.
    (Pikkus + suur + väike täht + number + erimärk)
    """
    return (
        is_correct_length(password)
        and includes_uppercase(password)
        and includes_lowercase(password)
        and includes_number(password)
        and includes_special(password)
    )
