"""审计日志工具函数"""

import uuid

from sqlalchemy.ext.asyncio import AsyncSession

from app.models.audit import AuditLog


async def audit_log(
    db: AsyncSession,
    action: str,
    actor_id: uuid.UUID,
    tenant_id: uuid.UUID | None = None,
    target_type: str | None = None,
    target_id: str | None = None,
    detail: str | None = None,
    ip_address: str | None = None,
) -> AuditLog:
    """记录一条敏感操作审计日志"""
    log = AuditLog(
        actor_id=actor_id,
        tenant_id=tenant_id,
        action=action,
        target_type=target_type,
        target_id=target_id,
        detail=detail,
        ip_address=ip_address,
    )
    db.add(log)
    await db.commit()
    return log
