"""签到 endpoint。

路由前缀 `/api/v1/signin`:
  GET  /status    今日签到状态 + 7 天档位
  POST /          执行今日签到

2026-07 下线 `POST /boost`(签到膨胀):它按固定 3000 金币补发、与广告实际收益脱钩。
签到弹窗里的「看广告膨胀」改与福利页看视频走同一条 reward_video 路径(按 eCPM 发,
`/ad/pangle-callback` → `/ad/reward-result` 取金额),奖励只剩「签到」+「看视频」两种。
"""
from __future__ import annotations

import logging

from fastapi import APIRouter, HTTPException, status

from app.api.deps import CurrentUser, DbSession
from app.repositories import signin as crud_signin
from app.schemas.welfare import SigninResultOut, SigninStatusOut

logger = logging.getLogger("shagua.signin")

router = APIRouter(prefix="/api/v1/signin", tags=["signin"])


@router.get("/status", response_model=SigninStatusOut, summary="今日签到状态")
def signin_status(user: CurrentUser, db: DbSession) -> SigninStatusOut:
    st = crud_signin.get_status(db, user.id)
    return SigninStatusOut.model_validate(st, from_attributes=True)


@router.post("", response_model=SigninResultOut, summary="执行今日签到")
def do_signin(user: CurrentUser, db: DbSession) -> SigninResultOut:
    try:
        record, balance = crud_signin.do_signin(db, user.id)
    except crud_signin.AlreadySignedError as e:
        raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="already signed today") from e

    logger.info(
        "signin ok user_id=%d day=%d streak=%d coin=%d",
        user.id, record.cycle_day, record.streak, record.coin_awarded,
    )
    return SigninResultOut(
        coin_awarded=record.coin_awarded,
        cycle_day=record.cycle_day,
        streak=record.streak,
        coin_balance=balance,
    )
