"""Central helper for writing project audit logs."""
from .models import ProjectLog


def log_project(project_id, actor, log_type, action, detail=""):
    """Record an audit-log entry for a project action.

    Safe to call from anywhere — swallows errors so logging never breaks the
    underlying mutation. `actor` may be a User instance or None.
    """
    if not project_id:
        return None
    try:
        return ProjectLog.objects.create(
            project_id=project_id,
            actor=actor if (actor and getattr(actor, "is_authenticated", False)) else None,
            log_type=log_type,
            action=action[:255],
            detail=detail or "",
        )
    except Exception:
        return None
