from sqlalchemy import String, Text, Integer, ForeignKey, func from sqlalchemy.orm import Mapped, mapped_column from datetime import datetime from typing import Optional from app.database import Base class AuditLog(Base): """ Append-only audit trail for admin actions and auth events. No DELETE endpoint — this table is immutable once written. """ __tablename__ = "audit_log" id: Mapped[int] = mapped_column(primary_key=True) actor_user_id: Mapped[Optional[int]] = mapped_column( Integer, ForeignKey("users.id"), nullable=True, index=True ) target_user_id: Mapped[Optional[int]] = mapped_column( Integer, ForeignKey("users.id", ondelete="SET NULL"), nullable=True, index=True ) action: Mapped[str] = mapped_column(String(100), nullable=False, index=True) detail: Mapped[Optional[str]] = mapped_column(Text, nullable=True) ip_address: Mapped[Optional[str]] = mapped_column(String(45), nullable=True) created_at: Mapped[datetime] = mapped_column( default=func.now(), server_default=func.now(), index=True )