from datetime import datetime

from sqlalchemy import BigInteger, DateTime, ForeignKey, Integer, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import Base, TimestampMixin


class UserResourceTotalStat(Base, TimestampMixin):
    """用户全局资源空间聚合表。"""

    __tablename__ = "user_resource_total_stats"
    __table_args__ = (
        UniqueConstraint("user_id", name="uq_user_resource_total_stats_user"),
    )

    id: Mapped[str] = mapped_column(String(32), primary_key=True)
    user_id: Mapped[str] = mapped_column(
        String(32), ForeignKey("users.id", ondelete="CASCADE"), index=True, nullable=False
    )

    active_size_bytes: Mapped[int] = mapped_column(BigInteger, default=0, nullable=False)
    deleted_size_bytes: Mapped[int] = mapped_column(BigInteger, default=0, nullable=False)
    total_generated_size_bytes: Mapped[int] = mapped_column(BigInteger, default=0, nullable=False)

    image_size_bytes: Mapped[int] = mapped_column(BigInteger, default=0, nullable=False)
    video_size_bytes: Mapped[int] = mapped_column(BigInteger, default=0, nullable=False)

    active_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    deleted_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    image_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    video_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)

    last_recalculated_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
