"""Audit convenience wrappers.

Historically these lived at ``apps.rep.audit`` and were shared by the REP
learning platform, the job board, and core user-management views. When the REP
LMS was retired in favour of Moodle, the helpers moved here so the surviving
callers (core, the relocated ``apps.alumni.careers`` job board, and the alumni
job proxy) keep a stable home. ``apps/rep/audit.py`` remains as a thin
re-export shim only for the duration of the REP teardown.

``target_app`` defaults to ``"rep"`` to preserve the tag on existing audit-log
rows written by the core user-management views; new callers may pass their own
(e.g. ``"careers"``).
"""
from __future__ import annotations

from typing import Any

from apps.core.audit.middleware import AuditMiddleware
from apps.core.audit.mixins import log_audit
from apps.core.audit.models import AuditLog


def audit_rep(
    *,
    actor,
    action: str,
    target_model: str,
    object_id,
    object_repr: str = "",
    changes: dict[str, Any] | None = None,
    request=None,
    target_app: str = "rep",
) -> None:
    ip = None
    ua = ""
    if request is not None:
        ip = AuditMiddleware._client_ip(request)
        ua = (request.META.get("HTTP_USER_AGENT") or "")[:512]
    log_audit(
        actor=actor,
        action=action,
        target_app=target_app,
        target_model=target_model[:64],
        object_id=str(object_id)[:64] if object_id is not None else "",
        object_repr=(object_repr or "")[:200],
        changes=changes,
        ip_address=ip,
        user_agent=ua,
    )


def action_create() -> str:
    return AuditLog.Action.CREATE


def action_update() -> str:
    return AuditLog.Action.UPDATE


def action_delete() -> str:
    return AuditLog.Action.DELETE
