from datetime import date, datetime

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

from app.models.base import Base, TimestampMixin


class UserResourceMonthStat(Base, TimestampMixin):
    """用户月份资源空间聚合表。"""

    __tablename__ = "user_resource_month_stats"
    __table_args__ = (
        UniqueConstraint("user_id", "stat_month", name="uq_user_resource_month_stats_user_month"),
    )

    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
    )
    stat_month: Mapped[date] = mapped_column(Date, 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)
