"""Audit helper for Communication platform."""
from __future__ import annotations

from typing import Any
from uuid import UUID

from sqlalchemy.ext.asyncio import AsyncSession

from app.models.foundation import CommunicationAuditLog
from app.repositories.foundation import AuditLogRepository


class AuditService:
    def __init__(self, session: AsyncSession) -> None:
        self.repo = AuditLogRepository(session)

    async def log(
        self,
        *,
        tenant_id: UUID,
        entity_type: str,
        entity_id: UUID | str,
        action: str,
        actor_id: str | None = None,
        changes: dict[str, Any] | None = None,
        detail: str | None = None,
    ) -> CommunicationAuditLog:
        entry = CommunicationAuditLog(
            tenant_id=tenant_id,
            entity_type=entity_type,
            entity_id=str(entity_id),
            action=action,
            actor_id=actor_id,
            changes=changes,
            detail=detail,
        )
        return await self.repo.add(entry)
