import { type NextRequest, NextResponse } from "next/server"
import { getMindsDBConnector } from "@/lib/mindsdb-connector"

export async function POST(request: NextRequest) {
  try {
    const body = await request.json()
    const { query, model = "gpt4", parameters = {}, useCache = true } = body

    if (!query || !query.trim()) {
      return NextResponse.json({ error: "AI查询不能为空" }, { status: 400 })
    }

    const mindsDB = getMindsDBConnector()

    const result = await mindsDB.aiQuery({
      query,
      model,
      parameters,
      useCache,
    })

    return NextResponse.json({
      success: true,
      result,
      timestamp: new Date().toISOString(),
    })
  } catch (error) {
    console.error("AI查询API错误:", error)
    return NextResponse.json({ error: "AI查询失败，请稍后重试" }, { status: 500 })
  }
}
