"""积分任务记录表"""
import enum
from datetime import date, datetime

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

from app.models.base import BaseModel


class PointTaskType(str, enum.Enum):
    QQ_COMMUNITY = "qq_community"
    BILIBILI_FOLLOW = "bilibili_follow"
    DAILY_CHECKIN = "daily_checkin"


class PointTask(BaseModel):
    """用户积分任务完成记录（QQ群、B站关注、每日签到）。"""

    __tablename__ = "point_tasks"
    __table_args__ = (
        UniqueConstraint("user_id", "task_type", "checkin_date", name="uq_point_task_user_type_date"),
    )

    user_id: Mapped[int] = mapped_column(
        BigInteger, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
    )
    task_type: Mapped[PointTaskType] = mapped_column(
        SAEnum(PointTaskType, values_callable=lambda e: [m.value for m in e]),
        nullable=False,
        index=True,
    )
    reward_points: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    # 用户提交的答案（QQ群主QQ号 / B站用户名），签到无需填写
    submitted_value: Mapped[str | None] = mapped_column(String(255), nullable=True)
    # 签到日期，用于每日签到去重
    checkin_date: Mapped[date | None] = mapped_column(Date, nullable=True, index=True)
    # 签到获得的随机积分值（便于展示）
    checkin_points: Mapped[int | None] = mapped_column(Integer, nullable=True)
    completed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
