import re


def deslugify(title):
    """
    Convert camelCase, PascalCase, or snake_case to human-readable title.
    Handles known acronyms (e.g., postgreSQLParameters -> PostgreSQL Parameters).
    """
    ACRONYMS = {
        # Database systems
        "SQL": "SQL",
        "MYSQL": "MySQL",
        "POSTGRESQL": "PostgreSQL",
        "MARIADB": "MariaDB",
        "MSSQL": "MSSQL",
        "MONGODB": "MongoDB",
        "REDIS": "Redis",
        # Protocols
        "HTTP": "HTTP",
        "HTTPS": "HTTPS",
        "FTP": "FTP",
        "SFTP": "SFTP",
        "SSH": "SSH",
        "TLS": "TLS",
        "SSL": "SSL",
        # APIs
        "API": "API",
        "REST": "REST",
        "GRPC": "gRPC",
        "GRAPHQL": "GraphQL",
        # Networking
        "URL": "URL",
        "URI": "URI",
        "FQDN": "FQDN",
        "DNS": "DNS",
        "IP": "IP",
        "TCP": "TCP",
        "UDP": "UDP",
        # Data formats
        "JSON": "JSON",
        "XML": "XML",
        "YAML": "YAML",
        "CSV": "CSV",
        "HTML": "HTML",
        "CSS": "CSS",
        # Hardware
        "CPU": "CPU",
        "RAM": "RAM",
        "GPU": "GPU",
        "SSD": "SSD",
        "HDD": "HDD",
        # Identifiers
        "ID": "ID",
        "UUID": "UUID",
        "GUID": "GUID",
        "ARN": "ARN",
        # Cloud providers
        "AWS": "AWS",
        "GCP": "GCP",
        "AZURE": "Azure",
        "IBM": "IBM",
        # Kubernetes/Cloud
        "DB": "DB",
        "PVC": "PVC",
        "PV": "PV",
        "VPN": "VPN",
        # Auth
        "OS": "OS",
        "LDAP": "LDAP",
        "SAML": "SAML",
        "OAUTH": "OAuth",
        "JWT": "JWT",
        # AWS Services
        "S3": "S3",
        "EC2": "EC2",
        "RDS": "RDS",
        "EBS": "EBS",
        "IAM": "IAM",
    }

    if "_" in title:
        # Handle snake_case
        title = title.replace("_", " ")
        words = title.split()
    else:
        # Handle camelCase/PascalCase with smart splitting
        # This regex splits on:
        # - Transition from lowercase to uppercase (camelCase)
        # - Transition from multiple uppercase to an uppercase followed by lowercase (SQLParameters -> SQL Parameters)
        words = re.findall(r"[A-Z]+(?=[A-Z][a-z]|\b)|[A-Z][a-z]+|[a-z]+|[0-9]+", title)

    # Merge adjacent words if they form a known compound acronym (e.g., postgre + SQL = PostgreSQL)
    merged_words = []
    i = 0
    while i < len(words):
        if i < len(words) - 1:
            # Check if current word + next word form a known acronym
            combined = (words[i] + words[i + 1]).upper()
            if combined in ACRONYMS:
                merged_words.append(combined)
                i += 2
                continue
        merged_words.append(words[i])
        i += 1

    # Capitalize each word, using proper casing for known acronyms
    result = []
    for word in merged_words:
        word_upper = word.upper()
        if word_upper in ACRONYMS:
            result.append(ACRONYMS[word_upper])
        else:
            result.append(word.capitalize())

    return " ".join(result)


def parse_disk_size_gib(value):
    """Parse disk size string (e.g., '10Gi', '100G') to GiB as integer."""
    if not value:
        return None
    value = str(value)
    # Handle Gi suffix (GiB)
    if value.endswith("Gi"):
        try:
            return int(value[:-2])
        except ValueError:
            return None
    # Handle G suffix (assume GB, convert to GiB approximately)
    if value.endswith("G"):
        try:
            return int(float(value[:-1]) * 0.931)  # GB to GiB
        except ValueError:
            return None
    try:
        return int(value)
    except ValueError:
        return None
