from sqlmodel import SQLModel, Field from typing import Optional from datetime import datetime from sqlalchemy import Column, DateTime, JSON import uuid class AuditLogBase(SQLModel): event_id: str = Field(index=True) # UUID for deduplication event_type: str = Field(max_length=50) # created|updated|completed|deleted user_id: str # String user identifier task_id: int # Reference to the affected task event_data: dict = Field(sa_column=Column(JSON)) # JSONB field for event data timestamp: datetime = Field(sa_column=Column(DateTime(timezone=True)), default_factory=lambda: datetime.now(datetime.UTC)) class AuditLog(AuditLogBase, table=True): """ Persistent record of all task events for a user. Contains id, event_id, event_type, user_id, task_id, event_data (JSONB), and timestamp. """ id: Optional[int] = Field(default=None, primary_key=True) event_id: str = Field(index=True, unique=True) # Unique constraint for deduplication event_type: str = Field(max_length=50) # created|updated|completed|deleted user_id: str # String user identifier task_id: int # Reference to the affected task event_data: dict = Field(sa_column=Column(JSON)) # JSONB field for event data timestamp: datetime = Field(sa_column=Column(DateTime(timezone=True)), default_factory=lambda: datetime.now(datetime.UTC)) class AuditLogCreate(AuditLogBase): pass class AuditLogRead(AuditLogBase): id: int timestamp: datetime class Config: from_attributes = True