import enum
from sqlalchemy import Column, Integer, String, Enum, DateTime, ForeignKey, Text
from sqlalchemy.sql import func
from app.core.database import Base

class LoginMethod(str, enum.Enum):
    UNIFIED_PAGE = "UNIFIED_PAGE"       # 统一认证自有页面
    CUSTOM_PAGE = "CUSTOM_PAGE"         # 用户自定义登录页面
    DIRECT_JUMP = "DIRECT_JUMP"         # 统一认证平台直接跳转
    INTER_PLATFORM = "INTER_PLATFORM"   # 平台间相互跳转
    UNKNOWN = "UNKNOWN"

class AuthType(str, enum.Enum):
    PASSWORD = "PASSWORD"               # 账号密码
    SMS = "SMS"                         # 短信验证码
    TICKET = "TICKET"                   # 票据置换
    TOKEN = "TOKEN"                     # Token 验证
    SSO = "SSO"                         # 单点登录
    OTHER = "OTHER"

class LoginLog(Base):
    __tablename__ = "login_logs"

    id = Column(Integer, primary_key=True, index=True)
    
    # User Info
    user_id = Column(Integer, ForeignKey("users.id"), nullable=True) # Maybe null if login failed
    mobile = Column(String(20), nullable=True, index=True)
    
    # Network Info
    ip_address = Column(String(50), nullable=True)
    location = Column(String(100), nullable=True) # e.g. "Shanghai, China"
    user_agent = Column(Text, nullable=True)
    
    # Context
    login_method = Column(Enum(LoginMethod), default=LoginMethod.UNIFIED_PAGE, nullable=False)
    auth_type = Column(Enum(AuthType), default=AuthType.PASSWORD, nullable=False)
    
    # Status
    is_success = Column(Integer, default=1) # 1: Success, 0: Failed
    failure_reason = Column(String(255), nullable=True)
    
    created_at = Column(DateTime(timezone=True), server_default=func.now(), index=True)

