from sqlalchemy import Column, Integer, String, Text, Boolean, DateTime, ForeignKey, Enum
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
import enum
from app.core.database import Base

class MessageType(str, enum.Enum):
    MESSAGE = "MESSAGE"
    NOTIFICATION = "NOTIFICATION"

class ContentType(str, enum.Enum):
    TEXT = "TEXT"
    IMAGE = "IMAGE"
    VIDEO = "VIDEO"
    FILE = "FILE"
    # 用户在应用内发起的“申请通知”，用于在私信界面展示通知样式（携带 action_url/action_text）
    USER_NOTIFICATION = "USER_NOTIFICATION"

class Message(Base):
    __tablename__ = "messages"

    id = Column(Integer, primary_key=True, index=True)
    
    sender_id = Column(Integer, ForeignKey("users.id"), nullable=True)
    receiver_id = Column(Integer, ForeignKey("users.id"), nullable=False, index=True)
    app_id = Column(Integer, ForeignKey("applications.id"), nullable=True)

    type = Column(Enum(MessageType), default=MessageType.MESSAGE, nullable=False)
    content_type = Column(Enum(ContentType), default=ContentType.TEXT, nullable=False)
    
    title = Column(String(255), nullable=False)
    content = Column(Text, nullable=False)
    
    action_url = Column(String(1000), nullable=True)
    action_text = Column(String(50), nullable=True)
    
    is_read = Column(Boolean, default=False, nullable=False)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    read_at = Column(DateTime(timezone=True), nullable=True)

    sender = relationship("User", foreign_keys=[sender_id], backref="sent_messages")
    receiver = relationship("User", foreign_keys=[receiver_id], backref="received_messages")
    app = relationship("Application", foreign_keys=[app_id])
