import re
from fastapi import HTTPException, status

def normalize_unique_name(value: str) -> str:
    """
    Normalize a unique_name into a lowercase, hyphen-safe slug.
    Ensures:
    - lowercase
    - no spaces (converted to hyphens)
    - only a-z, 0-9, hyphens, underscores
    """

    if not value or not value.strip():
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="unique_name cannot be empty."
        )

    # standard cleanup
    cleaned = value.strip().lower().replace(" ", "-")

    # enforce safe charset
    if not re.fullmatch(r"[a-z0-9\-_]+", cleaned):
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="unique_name may only contain lowercase letters, numbers, hyphens, and underscores."
        )

    return cleaned
