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

class UserStatus(str, enum.Enum):
    PENDING = "PENDING"      # Waiting for approval
    ACTIVE = "ACTIVE"        # Approved and active
    DISABLED = "DISABLED"    # Manually disabled by admin

class UserRole(str, enum.Enum):
    SUPER_ADMIN = "SUPER_ADMIN"
    DEVELOPER = "DEVELOPER"
    ORDINARY_USER = "ORDINARY_USER"

class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    mobile = Column(String(20), unique=True, index=True, nullable=False)
    password_hash = Column(String(128), nullable=False)
    salt = Column(String(64), nullable=True)

    name = Column(String(100), nullable=True, comment="User Chinese Name")
    english_name = Column(String(100), nullable=True, comment="User English Name (Pinyin)")

    organization_id = Column(Integer, ForeignKey("organizations.id"), nullable=True)
    organization = relationship("Organization", back_populates="users")
    
    status = Column(Enum(UserStatus), default=UserStatus.PENDING, nullable=False)
    role = Column(Enum(UserRole), default=UserRole.DEVELOPER, nullable=False)
    
    is_deleted = Column(Integer, default=0, nullable=False) # 0: False, 1: True (Soft Delete)
    
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now(), server_default=func.now())

