# inventory-backend/app/models/system.py
from app.extensions import db
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import datetime


# ==========================================
# 1. 系统用户表
# ==========================================
class SysUser(db.Model):
    """
    系统用户表
    对应数据库: sys_user
    username 字段存储格式约定: "真实姓名/登录账号" (例如: 张三/zhangsan01)
    """
    __tablename__ = 'sys_user'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(100), nullable=False)  # 存储 "张三/zhangsan"
    email = db.Column(db.String(100), unique=True)
    department = db.Column(db.String(100))
    role = db.Column(db.String(50))
    status = db.Column(db.String(20), default='active')
    password_hash = db.Column(db.Text)
    created_at = db.Column(db.DateTime, default=datetime.now)

    def set_password(self, password):
        """生成加密密码"""
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        """验证密码"""
        return check_password_hash(self.password_hash, password)

    def to_dict(self):
        """
        序列化为字典
        数据库存的是 '张三/zhangsan'
        前端需要的是 '张三(zhangsan)'
        """
        raw_name = self.username
        display_name = raw_name
        account_id = raw_name

        # 解析存储格式: Name/ID
        if '/' in raw_name:
            parts = raw_name.split('/')
            real_name = parts[0]
            acc_id = parts[1]
            # 格式化为前端展示格式: 张三(zhangsan01)
            display_name = f"{real_name}({acc_id})"
            # 单独提取账号ID (如果前端需要单独用)
            account_id = acc_id

        return {
            'id': self.id,
            'username': display_name,  # 列表显示: 张三(zhangsan01)
            'raw_username': self.username,  # 原始数据
            'account_id': account_id,  # 纯账号ID: zhangsan01
            'email': self.email,
            'department': self.department,
            'role': self.role,
            'status': self.status,
            'created_at': self.created_at.isoformat() if self.created_at else None
        }


# ==========================================
# 2. 系统日志表
# ==========================================
class SysLog(db.Model):
    """
    系统操作日志表
    对应数据库表: sys_log
    """
    __tablename__ = 'sys_log'

    id = db.Column(db.Integer, primary_key=True)
    op_time = db.Column(db.DateTime, default=datetime.now)
    op_user_name = db.Column(db.String(100))
    op_user_id = db.Column(db.String(50))
    module_name = db.Column(db.String(100))
    action_type = db.Column(db.String(50))
    target_table = db.Column(db.String(100))
    target_id = db.Column(db.Integer)
    description = db.Column(db.Text)
    ip_address = db.Column(db.String(50))

    def to_dict(self):
        return {
            'id': self.id,
            'op_time': self.op_time.isoformat() if self.op_time else None,
            'op_user_name': self.op_user_name,
            'module_name': self.module_name,
            'action_type': self.action_type,
            'description': self.description
        }


# ==========================================
# 3. 权限管理模型 (RBAC) - [新增]
# ==========================================

class SysMenu(db.Model):
    """系统菜单/页面表"""
    __tablename__ = 'sys_menu'
    id = db.Column(db.Integer, primary_key=True)
    parent_id = db.Column(db.Integer, default=0)
    name = db.Column(db.String(50), nullable=False)
    code = db.Column(db.String(100), unique=True, nullable=False)
    path = db.Column(db.String(200))
    sort_order = db.Column(db.Integer, default=0)
    is_visible = db.Column(db.Boolean, default=True)

    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'code': self.code,
            'path': self.path,
            'type': 'menu'  # 前端树形控件图标判断用
        }


class SysElement(db.Model):
    """页面元素/列定义表"""
    __tablename__ = 'sys_element'
    id = db.Column(db.Integer, primary_key=True)
    menu_code = db.Column(db.String(100), db.ForeignKey('sys_menu.code'))
    name = db.Column(db.String(100), nullable=False)
    code = db.Column(db.String(100), nullable=False)  # 如: unit_price
    element_type = db.Column(db.String(20), default='column')

    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'code': self.code,
            'menu_code': self.menu_code,
            'type': 'element',
            'element_type': self.element_type
        }


class SysRolePermission(db.Model):
    """角色权限关联表"""
    __tablename__ = 'sys_role_permission'
    id = db.Column(db.Integer, primary_key=True)
    role_code = db.Column(db.String(50), nullable=False)
    target_code = db.Column(db.String(100), nullable=False)  # menu_code 或 element_code
    type = db.Column(db.String(20), nullable=False)  # 'menu' 或 'element'