from sqlalchemy import Column, String, Text, Integer, Index
from app.models.base import BaseModel
import enum


class LogLevel(str, enum.Enum):
    DEBUG = "debug"
    INFO = "info"
    WARNING = "warning"
    ERROR = "error"


class LogType(str, enum.Enum):
    AGENT = "agent"      # 智能体调用
    SYSTEM = "system"    # 系统运行
    CHAT = "chat"       # 问答对话


class Log(BaseModel):
    __tablename__ = "logs"

    level = Column(String(20), default=LogLevel.INFO.value, index=True)  # debug/info/warning/error
    type = Column(String(20), default=LogType.SYSTEM.value, index=True)   # agent/system/chat
    user_id = Column(String(36), nullable=True, index=True)  # 关联用户
    request_id = Column(String(64), nullable=True, index=True)
    route = Column(String(255), nullable=True, index=True)
    method = Column(String(16), nullable=True, index=True)
    status_code = Column(Integer, nullable=True, index=True)
    error_type = Column(String(100), nullable=True)
    operation = Column(String(100), nullable=True, index=True)
    message = Column(Text, nullable=False)  # 日志内容
    details = Column(Text, nullable=True)  # 详细信息(JSON)
    source = Column(String(100), nullable=True)  # 来源模块
    duration_ms = Column(Integer, nullable=True)  # 执行耗时

    __table_args__ = (
        Index('idx_logs_type_level', 'type', 'level'),
        Index('idx_logs_created_at', 'created_at'),
        Index('idx_logs_request_id', 'request_id'),
        Index('idx_logs_operation_status', 'operation', 'status_code'),
    )
