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


class WorkflowTemplate(Base):
    """工作流模板表（用户分享的模板）"""
    __tablename__ = "workflow_templates"

    id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="模板ID")
    name = Column(String(100), nullable=False, comment="模板名称")
    description = Column(Text, comment="模板描述")
    category = Column(String(50), comment="分类: llm/data_processing/automation/integration/other")
    tags = Column(JSON, comment="标签列表")
    nodes = Column(JSON, nullable=False, comment="节点配置")
    edges = Column(JSON, nullable=False, comment="边配置")
    thumbnail = Column(String(500), comment="缩略图URL")
    is_public = Column(Boolean, default=True, comment="是否公开")
    is_featured = Column(Boolean, default=False, comment="是否精选")
    view_count = Column(Integer, default=0, comment="查看次数")
    use_count = Column(Integer, default=0, comment="使用次数")
    rating_count = Column(Integer, default=0, comment="评分次数")
    rating_avg = Column(Float, default=0.0, comment="平均评分")
    user_id = Column(CHAR(36), ForeignKey("users.id"), nullable=False, comment="创建者ID")
    created_at = Column(DateTime, default=func.now(), comment="创建时间")
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now(), comment="更新时间")

    # 关系
    user = relationship("User", backref="shared_templates")
    ratings = relationship("TemplateRating", back_populates="template", cascade="all, delete-orphan")
    favorites = relationship("TemplateFavorite", back_populates="template", cascade="all, delete-orphan")

    def __repr__(self):
        return f"<WorkflowTemplate(id={self.id}, name={self.name})>"


class TemplateRating(Base):
    """模板评分表"""
    __tablename__ = "template_ratings"

    id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="评分ID")
    template_id = Column(CHAR(36), ForeignKey("workflow_templates.id"), nullable=False, comment="模板ID")
    user_id = Column(CHAR(36), ForeignKey("users.id"), nullable=False, comment="用户ID")
    rating = Column(Integer, nullable=False, comment="评分: 1-5")
    comment = Column(Text, comment="评论")
    created_at = Column(DateTime, default=func.now(), comment="创建时间")
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now(), comment="更新时间")

    # 关系
    template = relationship("WorkflowTemplate", back_populates="ratings")
    user = relationship("User", backref="template_ratings")

    def __repr__(self):
        return f"<TemplateRating(id={self.id}, template_id={self.template_id}, rating={self.rating})>"


class TemplateFavorite(Base):
    """模板收藏表"""
    __tablename__ = "template_favorites"

    id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="收藏ID")
    template_id = Column(CHAR(36), ForeignKey("workflow_templates.id"), nullable=False, comment="模板ID")
    user_id = Column(CHAR(36), ForeignKey("users.id"), nullable=False, comment="用户ID")
    created_at = Column(DateTime, default=func.now(), comment="创建时间")

    # 关系
    template = relationship("WorkflowTemplate", back_populates="favorites")
    user = relationship("User", backref="template_favorites")

    def __repr__(self):
        return f"<TemplateFavorite(id={self.id}, template_id={self.template_id}, user_id={self.user_id})>"
