"""
用户积分汇总表
==============

记录用户当前可用积分及累计统计。

设计说明：
- 余额是实时计算的聚合值，查询时不需要 JOIN batches
- 每次充值/消费/过期都同步更新此表
- SELECT ... FOR UPDATE 锁定此表行，防止并发超扣
"""

import uuid

from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import BaseModelBigInt


class UserPoint(BaseModelBigInt):
    """用户积分汇总"""

    __tablename__ = "mjk_user_points"

    user_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        unique=True,
        nullable=False,
        comment="用户 ID",
    )

    balance: Mapped[int] = mapped_column(
        default=0,
        nullable=False,
        comment="当前积分余额（允许欠费为负）",
    )

    total_recharged: Mapped[int] = mapped_column(
        default=0,
        nullable=False,
        comment="累计充值积分",
    )

    total_consumed: Mapped[int] = mapped_column(
        default=0,
        nullable=False,
        comment="累计消费积分",
    )

    total_expired: Mapped[int] = mapped_column(
        default=0,
        nullable=False,
        comment="累计过期积分",
    )
