"""
岗位成员关联模型
"""
from datetime import datetime
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Boolean, UniqueConstraint, func
from sqlalchemy.orm import relationship
from app.models.base import BaseModel, SoftDeleteMixin


class PositionMember(BaseModel, SoftDeleteMixin):
    """岗位成员关联表"""
    __tablename__ = "position_members"
    
    # 添加唯一约束：同一岗位下同一用户只能有一条有效记录
    __table_args__ = (
        UniqueConstraint('position_id', 'user_id', 'is_deleted', name='uix_position_user'),
    )
    
    position_id = Column(Integer, ForeignKey("positions.id"), nullable=False, comment="岗位ID")
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, comment="用户ID")
    role = Column(String(50), comment="成员角色（预留字段）")
    joined_at = Column(DateTime, server_default=func.now(), comment="加入时间（北京时间）")
    
    # 关系
    position = relationship("Position", back_populates="members")
    user = relationship("User", back_populates="position_memberships")
