"""比价战绩里程碑领取记录表。

「记录比价战绩」每档(第 1~6 次)只能领一次,领取后写一行,(user_id, milestone)
唯一,防止重复领奖。解锁进度由 comparison_record 里 status='success' 的条数决定,
不存进度本身——只在这里记"哪几档已领"。仿 user_task 的一次性领取模型。
"""
from __future__ import annotations

from datetime import datetime

from sqlalchemy import DateTime, ForeignKey, Integer, UniqueConstraint, func
from sqlalchemy.orm import Mapped, mapped_column

from app.db.base import Base


class ComparisonMilestoneClaim(Base):
    __tablename__ = "comparison_milestone_claim"
    __table_args__ = (
        UniqueConstraint("user_id", "milestone", name="uq_compare_milestone_user"),
    )

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    user_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("user.id"), index=True, nullable=False
    )
    # 档位序号(1-based),见 app.core.rewards.RECORD_MILESTONES
    milestone: Mapped[int] = mapped_column(Integer, nullable=False)
    coin_awarded: Mapped[int] = mapped_column(Integer, nullable=False, default=0)

    claimed_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), server_default=func.now(), nullable=False
    )

    def __repr__(self) -> str:  # pragma: no cover
        return f"<ComparisonMilestoneClaim user_id={self.user_id} m={self.milestone}>"
