"""
AI 相关 API 路由
"""

from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from loguru import logger

from app.services.glm_service import glm_service


router = APIRouter(prefix="/api/ai", tags=["AI"])


class GenerateMetaRequest(BaseModel):
    """生成标题标签请求"""
    text: str


class GenerateMetaResponse(BaseModel):
    """生成标题标签响应"""
    title: str
    tags: list[str]


@router.post("/generate-meta", response_model=GenerateMetaResponse)
async def generate_meta(req: GenerateMetaRequest):
    """
    AI 生成视频标题和标签

    根据口播文案自动生成吸引人的标题和相关标签
    """
    if not req.text or not req.text.strip():
        raise HTTPException(status_code=400, detail="口播文案不能为空")

    try:
        logger.info(f"Generating meta for text: {req.text[:50]}...")
        result = await glm_service.generate_title_tags(req.text)
        return GenerateMetaResponse(
            title=result.get("title", ""),
            tags=result.get("tags", [])
        )
    except Exception as e:
        logger.error(f"Generate meta failed: {e}")
        raise HTTPException(status_code=500, detail=str(e))
