"""ID generation for GreyMatter entities.

All randomness is sourced from :func:`greymatter_core.security.fips.fips_choice`,
which routes through the OpenSSL FIPS provider when ``GM_FIPS_MODE=1``.
This ensures every generated ID satisfies FIPS 140-3 RNG requirements in
regulated deployments (CJIS, FedRAMP) without any caller-side changes.

Alphabet: lowercase ASCII + digits (36 chars).  An 8-character ID has
~41 bits of entropy — collision probability < 1 in 2 trillion.
"""
import string
from datetime import datetime

from greymatter_core.security.fips import fips_choice

ALPHABET = string.ascii_lowercase + string.digits


def _short_id(length: int = 8) -> str:
    """Generate a short alphanumeric ID using FIPS-aware randomness."""
    return "".join(fips_choice(ALPHABET) for _ in range(length))


def generate_pattern_id() -> str:
    """Generate pattern ID: pat-xxxxxxxx"""
    return f"pat-{_short_id(8)}"


def generate_knowledge_id() -> str:
    """Generate knowledge entry ID: kn-xxxxxxxx"""
    return f"kn-{_short_id(8)}"


def generate_session_id() -> str:
    """Generate session ID: YYYY-MM-DD-xxxxxxxx"""
    date_str = datetime.now().strftime("%Y-%m-%d")
    return f"{date_str}-{_short_id(8)}"


def generate_work_item_id() -> str:
    """Generate work item ID: wi-xxxxxxxx"""
    return f"wi-{_short_id(8)}"


def generate_standard_id(category: str) -> str:
    """Generate quality standard ID: qs-{category}-xxxxxxxx"""
    return f"qs-{category}-{_short_id(8)}"


def generate_build_run_id() -> str:
    """Generate build run ID: br-xxxxxxxx"""
    return f"br-{_short_id(8)}"


def generate_document_id() -> str:
    """Generate document ID: doc-xxxxxxxx"""
    return f"doc-{_short_id(8)}"


def generate_registry_event_id() -> str:
    """Generate session registry event ID: sre-xxxxxxxx"""
    return f"sre-{_short_id(8)}"


def generate_conversation_session_id() -> str:
    """Generate conversation session ID: cs-YYYY-MM-DD-xxxxxxxx"""
    date_str = datetime.now().strftime("%Y-%m-%d")
    return f"cs-{date_str}-{_short_id(8)}"


def generate_contact_id() -> str:
    """Generate contact ID: con-xxxxxxxx"""
    return f"con-{_short_id(8)}"


def generate_lineage_id() -> str:
    """Generate lineage event ID: lin-xxxxxxxx"""
    return f"lin-{_short_id(8)}"


def generate_mood_id() -> str:
    """Generate mood observation ID: mood-xxxxxxxx"""
    return f"mood-{_short_id(8)}"



def generate_id(prefix: str) -> str:
    """Generate a generic ID: {prefix}-xxxxxxxx"""
    return f"{prefix}-{_short_id(8)}"

