from __future__ import annotations from datetime import date, datetime from decimal import Decimal from typing import Any from uuid import UUID from sqlalchemy import ( ARRAY, BigInteger, Boolean, CheckConstraint, Computed, Date, DateTime, ForeignKey, Index, Integer, Numeric, PrimaryKeyConstraint, String, Text, UniqueConstraint, text, ) from sqlalchemy.dialects.postgresql import JSONB, TSVECTOR, UUID as PGUUID from sqlalchemy.orm import Mapped, mapped_column, relationship from app.db.base import Base from app.db.schemas import AUTH_SCHEMA, COMMERCE_SCHEMA, CONTENT_SCHEMA, PLATFORM_SCHEMA from app.db.enums import ( ATTRIBUTE_VALUE_TYPE_ENUM, BILLING_PERIOD_ENUM, CONTENT_KIND_ENUM, DEVICE_REQUEST_STATUS_ENUM, ENTITLEMENT_STATUS_ENUM, MEDIA_KIND_ENUM, MODULE_STATUS_ENUM, NOTIFICATION_DELIVERY_ENUM, PAYMENT_STATUS_ENUM, PLAN_TYPE_ENUM, RECORD_STATUS_ENUM, SUBSCRIPTION_STATUS_ENUM, AttributeValueType, BillingPeriod, ContentKind, DeviceRequestStatus, EntitlementStatus, MediaKind, ModuleStatus, NotificationDelivery, PaymentStatus, PlanType, RecordStatus, SubscriptionStatus, ) # ============================================================================= # PLATFORM MODELS # ============================================================================= class UserActivityLog(Base): __tablename__ = "user_activity_logs" __table_args__ = {"schema": PLATFORM_SCHEMA} id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()")) user_id: Mapped[UUID | None] = mapped_column(PGUUID(as_uuid=True), ForeignKey(f"{AUTH_SCHEMA}.app_users.id", ondelete="CASCADE")) session_token: Mapped[str | None] = mapped_column(Text) activity_type: Mapped[str] = mapped_column(Text, nullable=False) resource_type: Mapped[str | None] = mapped_column(Text) resource_id: Mapped[UUID | None] = mapped_column(PGUUID(as_uuid=True)) search_query: Mapped[str | None] = mapped_column(Text) metadata_json: Mapped[dict[str, Any]] = mapped_column("metadata", JSONB, nullable=False, server_default=text("'{}'::jsonb")) ip_address: Mapped[str | None] = mapped_column(Text) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=text("now()")) user: Mapped[AppUser | None] = relationship() class AdminAuditLog(Base): __tablename__ = "admin_audit_logs" __table_args__ = {"schema": PLATFORM_SCHEMA} id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()")) admin_id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), ForeignKey(f"{AUTH_SCHEMA}.app_users.id", ondelete="CASCADE"), nullable=False) action_type: Mapped[str] = mapped_column(Text, nullable=False) target_table: Mapped[str] = mapped_column(Text, nullable=False) target_record_id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), nullable=False) description: Mapped[str | None] = mapped_column(Text) ip_address: Mapped[str | None] = mapped_column(Text) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=text("now()")) admin: Mapped[AppUser] = relationship() class Notification(Base): __tablename__ = "notifications" __table_args__ = ( Index("idx_notifications_user_unread", "user_id", "is_read", text("created_at desc")), {"schema": PLATFORM_SCHEMA}, ) id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()")) user_id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), ForeignKey(f"{AUTH_SCHEMA}.app_users.id", ondelete="CASCADE"), nullable=False) channel: Mapped[str] = mapped_column(Text, nullable=False, server_default=text("'in_app'")) category: Mapped[str] = mapped_column(Text, nullable=False) title: Mapped[str] = mapped_column(Text, nullable=False) body: Mapped[str] = mapped_column(Text, nullable=False) action_url: Mapped[str | None] = mapped_column(Text) metadata_json: Mapped[dict[str, Any]] = mapped_column("metadata", JSONB, nullable=False, server_default=text("'{}'::jsonb")) is_read: Mapped[bool] = mapped_column(Boolean, nullable=False, server_default=text("false")) read_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True)) sent_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True)) delivery_status: Mapped[NotificationDelivery] = mapped_column(NOTIFICATION_DELIVERY_ENUM, nullable=False, server_default=text("'pending'")) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=text("now()")) user: Mapped[AppUser] = relationship(back_populates="notifications")