"""
用户相关数据模型
"""
from sqlalchemy import Column, String, Boolean, Integer, DateTime, Text, Date, JSON, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.dialects.mysql import CHAR, TINYINT
from .base import BaseModel
from datetime import datetime
from typing import Optional


class User(BaseModel):
    """用户表"""
    __tablename__ = "t_sys_user"
    
    username = Column(String(50), unique=True, nullable=False, comment="用户名")
    email = Column(String(100), unique=True, nullable=False, comment="邮箱")
    phone = Column(String(20), unique=True, nullable=True, comment="手机号")
    password_hash = Column(String(255), nullable=False, comment="密码哈希")
    avatar_url = Column(String(500), nullable=True, comment="头像URL")
    is_active = Column(Boolean, default=True, comment="是否激活")
    is_superuser = Column(Boolean, default=False, comment="是否超级管理员")
    last_login_at = Column(DateTime, nullable=True, comment="最后登录时间")
    last_login_ip = Column(String(45), nullable=True, comment="最后登录IP")
    failed_login_attempts = Column(Integer, default=0, comment="失败登录次数")
    locked_until = Column(DateTime, nullable=True, comment="锁定直到时间")
    is_deleted = Column(Boolean, default=False, comment="是否删除")
    
    # 关联关系
    profile = relationship("UserProfile", back_populates="user", uselist=False)
    roles = relationship("UserRole", back_populates="user")
    tokens = relationship("OAuthAccessToken", back_populates="user")
    login_logs = relationship("LoginLog", back_populates="user")


class UserProfile(BaseModel):
    """用户详情表"""
    __tablename__ = "t_sys_user_profile"
    
    user_id = Column(CHAR(36), ForeignKey("t_sys_user.id", ondelete="CASCADE"), nullable=False, comment="用户ID")
    real_name = Column(String(50), nullable=True, comment="真实姓名")
    gender = Column(TINYINT, nullable=True, comment="性别 0:未知 1:男 2:女")
    birth_date = Column(Date, nullable=True, comment="出生日期")
    address = Column(String(500), nullable=True, comment="地址")
    company = Column(String(100), nullable=True, comment="公司")
    department = Column(String(100), nullable=True, comment="部门")
    position = Column(String(100), nullable=True, comment="职位")
    extra_info = Column(JSON, nullable=True, comment="扩展信息")
    
    # 关联关系
    user = relationship("User", back_populates="profile")


class Role(BaseModel):
    """角色表"""
    __tablename__ = "t_sys_role"
    
    name = Column(String(50), unique=True, nullable=False, comment="角色名称")
    code = Column(String(50), unique=True, nullable=False, comment="角色代码")
    description = Column(String(500), nullable=True, comment="角色描述")
    is_active = Column(Boolean, default=True, comment="是否激活")
    
    # 关联关系
    users = relationship("UserRole", back_populates="role")
    permissions = relationship("RolePermission", back_populates="role")


class UserRole(BaseModel):
    """用户角色关系表"""
    __tablename__ = "t_sys_user_role"
    
    user_id = Column(CHAR(36), ForeignKey("t_sys_user.id", ondelete="CASCADE"), nullable=False, comment="用户ID")
    role_id = Column(CHAR(36), ForeignKey("t_sys_role.id", ondelete="CASCADE"), nullable=False, comment="角色ID")
    
    # 关联关系
    user = relationship("User", back_populates="roles")
    role = relationship("Role", back_populates="users")


class Permission(BaseModel):
    """权限表"""
    __tablename__ = "t_sys_permission"
    
    name = Column(String(100), nullable=False, comment="权限名称")
    code = Column(String(100), unique=True, nullable=False, comment="权限代码")
    description = Column(String(500), nullable=True, comment="权限描述")
    resource = Column(String(100), nullable=True, comment="资源")
    action = Column(String(50), nullable=True, comment="操作")
    is_active = Column(Boolean, default=True, comment="是否激活")
    
    # 关联关系
    roles = relationship("RolePermission", back_populates="permission")


class RolePermission(BaseModel):
    """角色权限关系表"""
    __tablename__ = "t_sys_role_permission"
    
    role_id = Column(CHAR(36), ForeignKey("t_sys_role.id", ondelete="CASCADE"), nullable=False, comment="角色ID")
    permission_id = Column(CHAR(36), ForeignKey("t_sys_permission.id", ondelete="CASCADE"), nullable=False, comment="权限ID")
    
    # 关联关系
    role = relationship("Role", back_populates="permissions")
    permission = relationship("Permission", back_populates="roles")


class Menu(BaseModel):
    """菜单表"""
    __tablename__ = "t_sys_menu"
    
    parent_id = Column(CHAR(36), nullable=True, comment="父菜单ID")
    name = Column(String(100), nullable=False, comment="菜单名称")
    path = Column(String(200), nullable=True, comment="路由路径")
    component = Column(String(200), nullable=True, comment="组件路径")
    icon = Column(String(100), nullable=True, comment="图标")
    sort_order = Column(Integer, default=0, comment="排序")
    menu_type = Column(TINYINT, default=1, comment="菜单类型 1:菜单 2:按钮")
    is_visible = Column(Boolean, default=True, comment="是否显示")
    is_active = Column(Boolean, default=True, comment="是否激活")
    permission_code = Column(String(100), nullable=True, comment="权限代码")


class RoleMenu(BaseModel):
    """角色菜单关系表"""
    __tablename__ = "t_sys_role_menu"
    
    role_id = Column(CHAR(36), ForeignKey("t_sys_role.id", ondelete="CASCADE"), nullable=False, comment="角色ID")
    menu_id = Column(CHAR(36), ForeignKey("t_sys_menu.id", ondelete="CASCADE"), nullable=False, comment="菜单ID")