from fastapi import APIRouter, Depends, HTTPException
from typing import Optional

from app.utils.openai import openai_client
from app.routes.user import get_current_active_user

# 创建路由器
router = APIRouter(prefix="/openai", tags=["openai"])


@router.post("/generate-data", response_model=dict)
async def generate_simulation_data(
    prompt: str,
    data_type: str = "text",
    current_user: dict = Depends(get_current_active_user)
):
    """生成仿真输入数据"""
    # 验证数据类型
    valid_types = ["text", "image", "structured"]
    if data_type not in valid_types:
        raise HTTPException(status_code=400, detail=f"Invalid data type. Valid types are: {', '.join(valid_types)}")
    
    # 生成数据
    result = openai_client.generate_simulation_data(prompt, data_type)
    if not result:
        raise HTTPException(status_code=500, detail="Failed to generate data from OpenAI")
    
    return result


@router.post("/describe-image", response_model=dict)
async def generate_image_description(
    image_url: str,
    current_user: dict = Depends(get_current_active_user)
):
    """生成图片描述"""
    # 生成图片描述
    description = openai_client.generate_image_description(image_url)
    if not description:
        raise HTTPException(status_code=500, detail="Failed to generate image description from OpenAI")
    
    return {"description": description}
