from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, DateTime, Text, JSON
from sqlalchemy.orm import relationship
from datetime import datetime
from database import Base

class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String, unique=True, index=True)
    email = Column(String, unique=True, index=True)
    hashed_password = Column(String)
    is_active = Column(Boolean, default=True)
    is_admin = Column(Boolean, default=False)
    
    # relationships
    owned_rooms = relationship("ChatRoom", back_populates="owner")
    sent_messages = relationship("Message", back_populates="sender")

class Friend(Base):
    __tablename__ = "friends"
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"))
    friend_id = Column(Integer, ForeignKey("users.id"))
    alias = Column(String, nullable=True) # 备注名

class ChatRoom(Base):
    __tablename__ = "chat_rooms"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, index=True)
    owner_id = Column(Integer, ForeignKey("users.id"))
    share_link = Column(String, unique=True, index=True)
    is_private = Column(Boolean, default=False) # 私聊还是群聊
    
    owner = relationship("User", back_populates="owned_rooms")
    members = relationship("RoomMember", back_populates="room")
    messages = relationship("Message", back_populates="room")

class RoomMember(Base):
    __tablename__ = "room_members"
    id = Column(Integer, primary_key=True, index=True)
    room_id = Column(Integer, ForeignKey("chat_rooms.id"))
    user_id = Column(Integer, ForeignKey("users.id"))
    
    room = relationship("ChatRoom", back_populates="members")
    user = relationship("User")

class RoomEmployee(Base):
    """聊天室中的数字员工"""
    __tablename__ = "room_employees"
    id = Column(Integer, primary_key=True, index=True)
    room_id = Column(Integer, ForeignKey("chat_rooms.id"))
    employee_id = Column(Integer, ForeignKey("digital_employees.id"))
    
    room = relationship("ChatRoom")
    employee = relationship("DigitalEmployee")

class Message(Base):
    __tablename__ = "messages"

    id = Column(Integer, primary_key=True, index=True)
    room_id = Column(Integer, ForeignKey("chat_rooms.id"))
    sender_id = Column(Integer, ForeignKey("users.id"))
    content = Column(Text)
    message_type = Column(String, default="text")  # text, emoji, image
    timestamp = Column(DateTime, default=datetime.utcnow)
    is_sensitive = Column(Boolean, default=False)
    
    room = relationship("ChatRoom", back_populates="messages")
    sender = relationship("User", back_populates="sent_messages")

class AIModel(Base):
    __tablename__ = "ai_models"
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, index=True) # 比如 gpt-3.5-turbo
    api_key = Column(String)
    base_url = Column(String)
    is_default = Column(Boolean, default=False)  # 是否为默认模型

class AITool(Base):
    __tablename__ = "ai_tools"
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, index=True)
    description = Column(Text)
    parameters_schema = Column(JSON) # Function calling 参数
    function_code = Column(Text, nullable=True) # Python 函数代码
    is_code_tool = Column(Boolean, default=False) # 是否是代码工具

class DigitalEmployee(Base):
    __tablename__ = "digital_employees"
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, index=True)
    model_id = Column(Integer, ForeignKey("ai_models.id"))
    system_prompt = Column(Text)
    tool_ids = Column(JSON) # [1, 2, 3] 关联到工具

class ToolCallLog(Base):
    """工具调用记录"""
    __tablename__ = "tool_call_logs"
    id = Column(Integer, primary_key=True, index=True)
    tool_id = Column(Integer, ForeignKey("ai_tools.id"))  # 调用的工具ID
    employee_id = Column(Integer, ForeignKey("digital_employees.id"))  # 调用的数字员工ID
    user_id = Column(Integer, ForeignKey("users.id"), nullable=True)  # 触发调用的用户ID
    room_id = Column(Integer, ForeignKey("chat_rooms.id"), nullable=True)  # 所在聊天室ID
    input_params = Column(JSON)  # 输入参数
    output_result = Column(Text)  # 输出结果
    status = Column(String)  # success, error
    error_message = Column(Text, nullable=True)  # 错误信息
    execution_time = Column(Integer)  # 执行时间（毫秒）
    timestamp = Column(DateTime, default=datetime.utcnow)  # 调用时间
    
    # relationships
    tool = relationship("AITool")
    employee = relationship("DigitalEmployee")
    user = relationship("User")
    room = relationship("ChatRoom")
