"""
Prompt 模板模型
"""
from sqlalchemy import Column, String, Text, Integer, Boolean, JSON

from app.base_model import BaseModel


class PromptTemplate(BaseModel):
    """
    Prompt 模板
    
    用于管理和复用 Prompt
    """
    __tablename__ = "ai_prompt_template"
    
    name = Column(String(100), nullable=False, comment="模板名称")
    code = Column(String(100), unique=True, nullable=False, comment="模板编码")
    category = Column(String(20), default="system", comment="分类: system/user/assistant/few_shot")
    description = Column(Text, default="", comment="描述")
    content = Column(Text, nullable=False, comment="模板内容")
    variables = Column(JSON, default=list, comment="变量定义列表")
    tags = Column(JSON, default=list, comment="标签列表")
    is_public = Column(Boolean, default=False, comment="是否公开")
    usage_count = Column(Integer, default=0, comment="使用次数")
    
    def render(self, variables: dict) -> str:
        """
        渲染模板
        
        Args:
            variables: 变量字典
            
        Returns:
            渲染后的内容
        """
        content = self.content or ""
        for key, value in variables.items():
            content = content.replace(f'{{{{{key}}}}}', str(value))
        return content
