"""
日志相关数据模型
"""
from sqlalchemy import Column, String, Boolean, Integer, DateTime, Text, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.dialects.mysql import CHAR
from .base import BaseModel
from datetime import datetime


class LoginLog(BaseModel):
    """登录日志表"""
    __tablename__ = "t_sys_login_log"
    
    user_id = Column(CHAR(36), ForeignKey("t_sys_user.id"), nullable=True, comment="用户ID")
    username = Column(String(50), nullable=False, comment="用户名")
    login_type = Column(String(20), nullable=True, comment="登录方式")
    ip_address = Column(String(45), nullable=True, comment="IP地址")
    user_agent = Column(Text, nullable=True, comment="用户代理")
    success = Column(Boolean, default=False, comment="是否成功")
    failure_reason = Column(String(200), nullable=True, comment="失败原因")
    login_at = Column(DateTime, default=datetime.utcnow, comment="登录时间")
    
    # 关联关系
    user = relationship("User", back_populates="login_logs")


class OperationLog(BaseModel):
    """操作日志表"""
    __tablename__ = "t_sys_operation_log"
    
    user_id = Column(CHAR(36), ForeignKey("t_sys_user.id"), nullable=True, comment="用户ID")
    username = Column(String(50), nullable=False, comment="用户名")
    operation_type = Column(String(50), nullable=True, comment="操作类型")
    target_type = Column(String(50), nullable=True, comment="目标类型")
    target_id = Column(CHAR(36), nullable=True, comment="目标ID")
    operation_detail = Column(Text, nullable=True, comment="操作详情")
    ip_address = Column(String(45), nullable=True, comment="IP地址")
    user_agent = Column(Text, nullable=True, comment="用户代理")
    operation_time = Column(DateTime, default=datetime.utcnow, comment="操作时间")
    
    # 关联关系
    user = relationship("User")


class SyncLog(BaseModel):
    """同步日志表"""
    __tablename__ = "t_sys_sync_log"
    
    app_id = Column(CHAR(36), ForeignKey("t_sys_app.id", ondelete="CASCADE"), nullable=False, comment="应用ID")
    sync_type = Column(String(50), nullable=True, comment="同步类型")
    sync_status = Column(String(20), nullable=True, comment="同步状态")
    records_count = Column(Integer, nullable=True, comment="记录数量")
    success_count = Column(Integer, nullable=True, comment="成功数量")
    failure_count = Column(Integer, nullable=True, comment="失败数量")
    error_message = Column(Text, nullable=True, comment="错误信息")
    start_time = Column(DateTime, nullable=True, comment="开始时间")
    end_time = Column(DateTime, nullable=True, comment="结束时间")
    
    # 关联关系
    app = relationship("App", back_populates="sync_logs")