"""
积分批次表
==========

每笔充值产生一个批次，记录初始积分、剩余积分、过期时间。

消费时按过期时间升序扣减（FIFO，先充先用）。
"""

import uuid
from datetime import datetime

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

from app.models.base import BaseModelBigInt


class PointBatch(BaseModelBigInt):
    """积分批次"""

    __tablename__ = "mjk_point_batches"

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

    amount: Mapped[int] = mapped_column(
        default=0,
        nullable=False,
        comment="初始积分",
    )

    remaining: Mapped[int] = mapped_column(
        default=0,
        nullable=False,
        comment="剩余可用积分",
    )

    expired_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        nullable=False,
        comment="过期时间（created_at + 180 天）",
    )

    source: Mapped[str] = mapped_column(
        String(32),
        default="wxpay",
        nullable=False,
        comment="来源：wxpay / invite / gift / compensation",
    )
