"""积分提现记录表"""
import enum
from datetime import datetime

from sqlalchemy import BigInteger, DateTime, ForeignKey, Integer, String
from sqlalchemy import Enum as SAEnum
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import BaseModel


class PointWithdrawalStatus(str, enum.Enum):
    PENDING = "pending"
    COMPLETED = "completed"
    REJECTED = "rejected"


class PointWithdrawal(BaseModel):
    """用户积分提现到余额记录。"""

    __tablename__ = "point_withdrawals"

    user_id: Mapped[int] = mapped_column(
        BigInteger, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
    )
    points_amount: Mapped[int] = mapped_column(Integer, nullable=False, comment="提现积分数量")
    fee_points: Mapped[int] = mapped_column(Integer, nullable=False, comment="手续费积分")
    credited_cents: Mapped[int] = mapped_column(Integer, nullable=False, comment="到账余额，单位分")
    status: Mapped[PointWithdrawalStatus] = mapped_column(
        SAEnum(PointWithdrawalStatus, values_callable=lambda e: [m.value for m in e]),
        default=PointWithdrawalStatus.COMPLETED,
        nullable=False,
        index=True,
    )
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    processed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
