from datetime import datetime, timezone from uuid import UUID, uuid4 from sqlalchemy import DateTime, ForeignKey, Index, Integer, String, Text, UniqueConstraint from sqlalchemy.dialects.postgresql import UUID as PG_UUID, JSONB from sqlalchemy.orm import Mapped, mapped_column from .base import Base def _now() -> datetime: return datetime.now(timezone.utc) class User(Base): __tablename__ = "users" id: Mapped[UUID] = mapped_column( PG_UUID(as_uuid=True), primary_key=True, default=uuid4 ) username: Mapped[str] = mapped_column( String(32), unique=True, nullable=False, index=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), default=_now, nullable=False ) class Chat(Base): __tablename__ = "chats" id: Mapped[UUID] = mapped_column( PG_UUID(as_uuid=True), primary_key=True, default=uuid4 ) title: Mapped[str] = mapped_column(String(100), nullable=False) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), default=_now, nullable=False ) __table_args__ = (Index("idx_chats_created_id", "created_at", "id"),) class ChatMember(Base): __tablename__ = "chat_members" chat_id: Mapped[UUID] = mapped_column( PG_UUID(as_uuid=True), ForeignKey("chats.id", ondelete="CASCADE"), primary_key=True, ) user_id: Mapped[UUID] = mapped_column( PG_UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), primary_key=True, ) __table_args__ = (Index("idx_chat_members_user", "user_id", "chat_id"),) class Message(Base): __tablename__ = "messages" id: Mapped[UUID] = mapped_column( PG_UUID(as_uuid=True), primary_key=True, default=uuid4 ) chat_id: Mapped[UUID] = mapped_column( PG_UUID(as_uuid=True), ForeignKey("chats.id", ondelete="CASCADE"), nullable=False, ) sender_id: Mapped[UUID] = mapped_column( PG_UUID(as_uuid=True), ForeignKey("users.id"), nullable=False ) text: Mapped[str] = mapped_column(Text, nullable=False) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), default=_now, nullable=False ) __table_args__ = ( Index("idx_messages_chat_created_id", "chat_id", "created_at", "id"), ) class SessionRow(Base): __tablename__ = "sessions" id: Mapped[UUID] = mapped_column( PG_UUID(as_uuid=True), primary_key=True, default=uuid4 ) user_id: Mapped[UUID] = mapped_column( PG_UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, ) token_hash: Mapped[str] = mapped_column( String(64), unique=True, nullable=False, index=True ) expires_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), nullable=False, index=True ) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), default=_now, nullable=False ) class IdempotencyRecord(Base): __tablename__ = "idempotency_keys" id: Mapped[UUID] = mapped_column( PG_UUID(as_uuid=True), primary_key=True, default=uuid4 ) user_id: Mapped[UUID] = mapped_column( PG_UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False ) key: Mapped[str] = mapped_column(String(128), nullable=False) request_hash: Mapped[str] = mapped_column(String(64), nullable=False) response_body: Mapped[str] = mapped_column(Text, nullable=False) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), default=_now, nullable=False ) __table_args__ = ( UniqueConstraint("user_id", "key", name="uq_idempotency_user_key"), ) class Attachment(Base): __tablename__ = "attachments" id: Mapped[UUID] = mapped_column( PG_UUID(as_uuid=True), primary_key=True, default=uuid4 ) chat_id: Mapped[UUID] = mapped_column( PG_UUID(as_uuid=True), ForeignKey("chats.id", ondelete="CASCADE"), nullable=False ) owner_id: Mapped[UUID] = mapped_column( PG_UUID(as_uuid=True), ForeignKey("users.id"), nullable=False ) filename: Mapped[str] = mapped_column(String(255), nullable=False) content_type: Mapped[str] = mapped_column(String(100), nullable=False) size_bytes: Mapped[int] = mapped_column(Integer, nullable=False) sha256: Mapped[str] = mapped_column(String(64), nullable=False) # ИЗМЕНЕНО: по умолчанию "queued", так как теперь обработка асинхронная status: Mapped[str] = mapped_column(String(20), default="queued", nullable=False) error_code: Mapped[str] = mapped_column(String(50), nullable=True) s3_key: Mapped[str] = mapped_column(String(500), nullable=False) # ДОБАВЛЕНО: колонка для хранения метаданных миниатюр variants: Mapped[list] = mapped_column(JSONB, default=list, nullable=False) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), default=_now, nullable=False ) updated_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), default=_now, onupdate=_now, nullable=False ) class MessageAttachment(Base): __tablename__ = "message_attachments" message_id: Mapped[UUID] = mapped_column( PG_UUID(as_uuid=True), ForeignKey("messages.id", ondelete="CASCADE"), primary_key=True ) attachment_id: Mapped[UUID] = mapped_column( PG_UUID(as_uuid=True), ForeignKey("attachments.id", ondelete="CASCADE"), primary_key=True )