""" Modello AuditLog – immutabile per compliance e tracciabilità. """ import uuid from datetime import datetime from sqlalchemy import ( BigInteger, DateTime, ForeignKey, Index, String, Text, func, ) from sqlalchemy.dialects.postgresql import INET, JSONB, UUID from sqlalchemy.orm import Mapped, mapped_column from app.database import Base class AuditLog(Base): __tablename__ = "audit_log" id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True) tenant_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("tenants.id"), nullable=True ) user_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("users.id"), nullable=True ) action: Mapped[str] = mapped_column(String(100), nullable=False) resource_type: Mapped[str | None] = mapped_column(String(100), nullable=True) resource_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True) ip_address: Mapped[str | None] = mapped_column(INET, nullable=True) user_agent: Mapped[str | None] = mapped_column(Text, nullable=True) payload: Mapped[dict | None] = mapped_column(JSONB, nullable=True) outcome: Mapped[str] = mapped_column(String(20), nullable=False, default="success") occurred_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), nullable=False, server_default=func.now() ) __table_args__ = ( Index("idx_audit_tenant_date", "tenant_id", "occurred_at"), Index("idx_audit_user", "user_id", "occurred_at"), Index("idx_audit_action", "action"), ) def __repr__(self) -> str: return f""