"""
系统配置数据模型
"""
from datetime import datetime
from sqlalchemy import Column, BigInteger, String, Text, Integer, DateTime, Boolean, Index
from sqlalchemy.dialects.postgresql import JSONB
from app.db.base import Base


class SystemConfig(Base):
    """系统配置表"""

    __tablename__ = "system_configs"

    id = Column(BigInteger, primary_key=True, index=True)
    config_key = Column(String(100), unique=True, nullable=False, index=True, comment="配置键")
    config_name = Column(String(200), nullable=False, comment="配置名称")
    config_value = Column(Text, nullable=True, comment="配置值")
    value_type = Column(String(20), default="string", nullable=False, comment="值类型: string/number/boolean/json")
    category = Column(String(50), nullable=False, index=True, comment="配置分类")
    description = Column(Text, nullable=True, comment="配置描述")
    is_system = Column(Boolean, default=False, nullable=False, comment="是否系统配置")
    is_encrypted = Column(Boolean, default=False, nullable=False, comment="是否加密存储")
    validation_rule = Column(Text, nullable=True, comment="验证规则(JSON)")
    options = Column(JSONB, nullable=True, comment="可选值配置")
    default_value = Column(Text, nullable=True, comment="默认值")
    sort_order = Column(Integer, default=0, nullable=False, comment="排序序号")
    is_active = Column(Boolean, default=True, nullable=False, comment="是否启用")
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    updated_by = Column(BigInteger, nullable=True, comment="更新人ID")

    # 索引
    __table_args__ = (
        Index("idx_system_config_category", "category"),
        Index("idx_system_config_active", "is_active"),
    )

    def __repr__(self):
        return f"<SystemConfig(id={self.id}, config_key={self.config_key}, config_name={self.config_name})>"
