from sqlalchemy import Column, String, Text, Integer, ForeignKey, Boolean
from sqlalchemy.orm import relationship
from app.models.base import BaseModel


class ForumPost(BaseModel):
    __tablename__ = "forum_posts"

    user_id = Column(String(36), ForeignKey("users.id"), nullable=False, index=True)
    title = Column(String(500), nullable=False)
    content = Column(Text, nullable=False)
    category = Column(String(100), nullable=True)  # instruction, discussion, question
    is_executed = Column(Boolean, default=False)
    execution_result = Column(Text, nullable=True)
    reply_count = Column(Integer, default=0)

    replies = relationship("ForumReply", back_populates="post", cascade="all, delete-orphan")


class ForumReply(BaseModel):
    __tablename__ = "forum_replies"

    post_id = Column(String(36), ForeignKey("forum_posts.id"), nullable=False, index=True)
    user_id = Column(String(36), ForeignKey("users.id"), nullable=True)
    agent_id = Column(String(36), ForeignKey("agents.id"), nullable=True)
    content = Column(Text, nullable=False)
    is_ai_reply = Column(Boolean, default=False)

    post = relationship("ForumPost", back_populates="replies")
    agent = relationship("Agent", back_populates="replies")
