from datetime import datetime
from sqlalchemy import Column, Integer, String, DateTime, JSON
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class ProjectMapping(Base):
    """项目映射模型"""
    __tablename__ = "project_mappings"
    
    id = Column(Integer, primary_key=True, index=True)
    repository_name = Column(String(255), nullable=False, unique=True, index=True)
    default_job = Column(String(255), nullable=False)
    branch_jobs = Column(JSON, nullable=False, default=list)  # [{"branch": "dev", "job": "dev-build"}]
    branch_patterns = Column(JSON, nullable=False, default=list)  # [{"pattern": "feature/*", "job": "feature-build"}]
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    
    def __repr__(self):
        return f"<ProjectMapping(id={self.id}, repository={self.repository_name}, default_job={self.default_job})>" 