"""
工作流版本模型
"""
from sqlalchemy import Column, String, Text, Integer, DateTime, JSON, ForeignKey, func
from sqlalchemy.dialects.mysql import CHAR
from sqlalchemy.orm import relationship
from app.core.database import Base
import uuid


class WorkflowVersion(Base):
    """工作流版本表"""
    __tablename__ = "workflow_versions"

    id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="版本ID")
    workflow_id = Column(CHAR(36), ForeignKey("workflows.id"), nullable=False, comment="工作流ID")
    version = Column(Integer, nullable=False, comment="版本号")
    name = Column(String(100), nullable=False, comment="工作流名称")
    description = Column(Text, comment="描述")
    nodes = Column(JSON, nullable=False, comment="节点配置")
    edges = Column(JSON, nullable=False, comment="边配置")
    status = Column(String(20), default="draft", comment="状态: draft/published/running/stopped")
    created_by = Column(CHAR(36), ForeignKey("users.id"), comment="创建者ID")
    created_at = Column(DateTime, default=func.now(), comment="创建时间")
    comment = Column(Text, comment="版本备注")

    # 关系
    workflow = relationship("Workflow", backref="versions")
    creator = relationship("User", foreign_keys=[created_by])

    def __repr__(self):
        return f"<WorkflowVersion(id={self.id}, workflow_id={self.workflow_id}, version={self.version})>"
