"""
火山引擎音视频字幕 API 路由
============================

提供自动字幕打轴功能。
"""

import logging

from fastapi import APIRouter, Depends, HTTPException, Request

from app.api.deps import get_current_user
from app.core.exceptions import PlatformError
from app.models.user import User
from app.schemas.caption import (
    AutoAlignSubmitRequest,
)
from app.schemas.common import success_response
from app.services.volcengine_caption_service import (
    get_caption_service,
)

logger = logging.getLogger(__name__)
router = APIRouter(prefix="/caption", tags=["Caption"])















@router.post("/ata/align")
async def auto_align_caption(
    request_body: AutoAlignSubmitRequest,
    request: Request,
    max_wait_time: int = 120,
    current_user: User = Depends(get_current_user),
):
    """
    自动字幕打轴（完整流程）

    提交打轴任务并轮询结果，直接返回最终数据。
    """
    try:
        logger.info(f"[Caption API] Auto align request: audio_url={request_body.audio_url[:50]}...")
        service = await get_caption_service(request)
        result = await service.auto_align_caption(
            audio_url=request_body.audio_url,
            audio_text=request_body.audio_text,
            caption_type=request_body.caption_type,
            sta_punc_mode=request_body.sta_punc_mode,
            max_wait_time=max_wait_time,
        )
        logger.info(
            f"[Caption API] Auto align result: utterances_count={len(result.utterances) if result.utterances else 0}"
        )
        if result.utterances:
            logger.info(f"[Caption API] First utterance: {result.utterances[0]}")

        # 手动序列化为字典，确保嵌套模型正确处理
        response_data = {
            "code": 0,
            "message": "Success",
            "duration": result.duration,
            "utterances": [
                {
                    "text": u.text,
                    "start_time": u.start_time,
                    "end_time": u.end_time,
                }
                for u in (result.utterances or [])
            ],
        }
        logger.info(f"[Caption API] Response data: {response_data}")
        return success_response(data=response_data)

    except PlatformError as e:
        logger.error(f"自动打轴失败: {e}")
        raise
    except Exception as e:
        logger.error(f"自动打轴异常: {e}")
        raise HTTPException(status_code=500, detail="字幕打轴失败，请稍后重试")






