from fastapi import APIRouter, HTTPException

from app.schemas.captcha import CaptchaResponse, CaptchaVerifyRequest, CaptchaTokenResponse
from app.services.captcha import generate_slider_captcha, verify_slider_captcha

router = APIRouter(prefix="/captcha", tags=["captcha"])


@router.get("/slider", response_model=CaptchaResponse)
async def get_slider_captcha():
    data = await generate_slider_captcha()
    return data


@router.post("/verify", response_model=CaptchaTokenResponse)
async def verify_captcha(req: CaptchaVerifyRequest):
    token = await verify_slider_captcha(req.captcha_id, req.x_offset)
    if not token:
        raise HTTPException(status_code=400, detail="验证失败，请重试")
    return CaptchaTokenResponse(token=token)
