"""PII masking helpers (DPDP) — partial reveal used in bulk/list responses.

Detail views / modals return the full value; lists show a partially-masked form
(first + last few chars kept) so the record is still recognisable but the full
PII isn't exposed in bulk.
"""
import re


def mask_phone(value) -> str:
    """9393939272 -> '939****272' (keeps first 3 and last 3 digits)."""
    if not value:
        return ""
    digits = re.sub(r"\D", "", str(value))
    if len(digits) <= 6:
        return "*" * len(digits) if digits else ""
    return digits[:3] + "*" * (len(digits) - 6) + digits[-3:]


def mask_email(value) -> str:
    """henasrivastav@alphabridge.com -> 'hen****@alphabridge.com' (first 3 of the
    local part + full domain kept)."""
    if not value or "@" not in str(value):
        return "" if not value else "*" * len(str(value))
    local, _, domain = str(value).partition("@")
    head = local[:3] if len(local) >= 3 else (local[:1] or "")
    return f"{head}****@{domain}"
