import uuid from typing import Any from sqlalchemy import JSON, ForeignKey, String, Uuid from sqlalchemy.orm import Mapped, mapped_column from app.db.base import Base, PkMixin, TimestampMixin class AuditEvent(Base, PkMixin, TimestampMixin): """Append-only domain/security event. Never stores raw financial payloads.""" __tablename__ = "audit_events" __table_args__ = ( {"comment": "Append-only audit trail; updates and deletes are blocked by trigger in PG."}, ) space_id: Mapped[uuid.UUID | None] = mapped_column( Uuid, ForeignKey("financial_spaces.id", ondelete="SET NULL"), nullable=True, index=True ) user_id: Mapped[uuid.UUID | None] = mapped_column( Uuid, ForeignKey("users.id", ondelete="SET NULL"), nullable=True, index=True ) event_type: Mapped[str] = mapped_column(String(64), nullable=False) detail: Mapped[dict[str, Any] | None] = mapped_column(JSON, nullable=True)