import OpenAI from "openai"
import { ensure } from "../utils"

const apiKey = process.env.OPENAI_API_KEY
ensure(apiKey, "OPENAI_API_KEY environment variable is required")

const client = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
})

export const searchWeb = async (query: string) => {
  const response = await client.responses.create({
    model: "gpt-4o-mini",
    tools: [
      {
        type: "web_search_preview",
        search_context_size: "low",
        user_location: {
          type: "approximate",
          country: "US",
          city: "San Francisco",
          region: "California",
        },
      },
    ],
    input: `Search the web for: ${query}`,
  })

  await Bun.sleep(10000)

  return response.output_text
}
