from database.database import db

class ClassMapping(db.Model):
    """Class name mappings for training project details (3NF)"""
    __tablename__ = 'class_mapping'
    
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    project_details_id = db.Column(db.Integer, db.ForeignKey('training_project_details.id', ondelete='CASCADE'), nullable=False)
    label_studio_project_id = db.Column(db.Integer, db.ForeignKey('label_studio_project.project_id', ondelete='CASCADE'), nullable=False)
    source_class = db.Column(db.String(255), nullable=False)
    target_class = db.Column(db.String(255), nullable=False)
    
    # Unique constraint: each source class can only be mapped once per project details AND label studio project
    __table_args__ = (
        db.UniqueConstraint('project_details_id', 'label_studio_project_id', 'source_class', name='uq_class_mapping'),
    )
    
    def to_dict(self):
        return {
            'id': self.id,
            'project_details_id': self.project_details_id,
            'label_studio_project_id': self.label_studio_project_id,
            'source_class': self.source_class,
            'target_class': self.target_class
        }
