"""
Audit Log Model

操作审计日志模型，用于记录系统中的所有关键操作。
"""

from typing import Optional, Dict, Any
from datetime import datetime
import uuid
from sqlmodel import SQLModel, Field, Column, JSON, Index


class AuditLogDB(SQLModel, table=True):
    """审计日志表"""
    __tablename__ = "audit_logs"

    id: str = Field(default_factory=lambda: str(uuid.uuid4()), primary_key=True)

    # 用户信息
    user_id: Optional[str] = Field(None, index=True, description="操作用户 ID")
    username: Optional[str] = Field(None, description="操作用户名（冗余存储）")

    # 操作信息
    action: str = Field(..., index=True, description="操作类型，如 user.create, project.delete")
    resource_type: Optional[str] = Field(None, index=True, description="资源类型，如 user, project, task")
    resource_id: Optional[str] = Field(None, description="资源 ID")

    # 请求信息
    ip_address: Optional[str] = Field(None, description="请求 IP 地址")
    user_agent: Optional[str] = Field(None, description="用户代理")

    # 详细信息
    details: Optional[Dict[str, Any]] = Field(default_factory=dict, sa_column=Column(JSON), description="详细数据（JSON）")

    # 时间戳
    created_at: float = Field(default_factory=lambda: datetime.now().timestamp(), description="创建时间")
