"""
文件服务接口 - 提供文件下载
"""
import os
from fastapi import APIRouter, HTTPException
from fastapi.responses import FileResponse
from loguru import logger

from app.config import settings

router = APIRouter()


@router.get("/files/{file_id}")
async def download_file(file_id: str):
    """
    下载文件（供 Coze 工作流访问）
    
    文件路径: uploads/{file_id}.pdf
    """
    # 构建文件路径
    file_path = os.path.join(settings.UPLOAD_DIR, f"{file_id}.pdf")
    
    # 检查文件是否存在
    if not os.path.exists(file_path):
        logger.error(f"File not found: {file_path}")
        raise HTTPException(status_code=404, detail="文件不存在")
    
    logger.info(f"Serving file: {file_path}")
    
    return FileResponse(
        path=file_path,
        filename=f"{file_id}.pdf",
        media_type="application/pdf"
    )
