import uuid from datetime import datetime from sqlalchemy import DateTime, ForeignKey, ForeignKeyConstraint, Index, String, Text, UniqueConstraint, func from sqlalchemy.dialects.postgresql import JSONB, UUID from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column class Base(DeclarativeBase): pass class User(Base): __tablename__ = "users" id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) username: Mapped[str] = mapped_column(String(32), nullable=False, unique=True) display_name: Mapped[str] = mapped_column(String(100), nullable=False) password_hash: Mapped[str] = mapped_column(Text, nullable=False) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now()) class Chat(Base): __tablename__ = "chats" id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) title: Mapped[str] = mapped_column(String(100), nullable=False) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now()) class ChatMember(Base): __tablename__ = "chat_members" __table_args__ = ( Index("chat_members_user_id_idx", "user_id", "chat_id"), ) chat_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("chats.id", ondelete="CASCADE"), primary_key=True ) user_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("users.id", ondelete="RESTRICT"), primary_key=True ) class Message(Base): __tablename__ = "messages" __table_args__ = ( ForeignKeyConstraint( ["chat_id", "sender_id"], ["chat_members.chat_id", "chat_members.user_id"], name="messages_sender_is_chat_member_fk", ondelete="CASCADE", ), Index("messages_history_idx", "chat_id", "created_at", "id"), ) id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) chat_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), nullable=False) sender_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), nullable=False) text: Mapped[str] = mapped_column(String(4000), nullable=False) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now()) class IdempotencyRecord(Base): """Shared, transactional result cache for retried message POST requests.""" __tablename__ = "idempotency_records" __table_args__ = ( UniqueConstraint( "user_id", "method", "path", "idempotency_key", name="idempotency_records_scope_key_uq", ), Index("idempotency_records_expires_at_idx", "expires_at"), ) id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) user_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False ) method: Mapped[str] = mapped_column(String(8), nullable=False) path: Mapped[str] = mapped_column(String(200), nullable=False) idempotency_key: Mapped[str] = mapped_column(String(128), nullable=False) request_hash: Mapped[str] = mapped_column(String(64), nullable=False) response_body: Mapped[dict] = mapped_column(JSONB, nullable=False) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now()) expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)