from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Boolean
from sqlalchemy.orm import relationship
from app.core.database import Base

class AppUserMapping(Base):
    __tablename__ = "app_user_mappings"

    id = Column(Integer, primary_key=True, index=True)
    app_id = Column(Integer, ForeignKey("applications.id"), nullable=False)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    
    # The username/email/id in the target application
    mapped_key = Column(String(100), nullable=True)
    
    # Optional email in the target application
    mapped_email = Column(String(100), nullable=True)

    # Status: True = Active, False = Disabled
    is_active = Column(Boolean, default=True, nullable=False)

    # Relationships
    application = relationship("Application")
    user = relationship("User")

    __table_args__ = (
        UniqueConstraint('app_id', 'user_id', name='uq_app_user'),
        UniqueConstraint('app_id', 'mapped_key', name='uq_app_mapped_key'),
        UniqueConstraint('app_id', 'mapped_email', name='uq_app_mapped_email'),
    )

