import { tool } from "@openai/agents"
import z from "zod"
import type { ImageOverlay } from "./types"
import { searchImages } from "pixabay-api"
import { getGeminiResponse } from "./ai"

const pixabayApiKey = "51428355-fea6dad6a1cb56273345b23b1"
export const tools = [
  tool({
    name: "create an image overlay",
    description: "Find an image to overlay on a video",
    parameters: z.object({
      whereToOverlay: z
        .string()
        .describe(
          "Where to overlay the image (e.g., 'in the red box', 'covering the hand', 'on the left side')"
        ),
      imageQuery: z.string().describe("Search term for image"),
    }),
    async execute(input, context) {
      const response = await searchImages(pixabayApiKey, input.imageQuery, { per_page: 10 })
      const hit = response.hits[0]!

      const image = await Bun.file("public/whiteboard.png").arrayBuffer()
      const boundingBox = await getGeminiResponse(
        image,
        `Find the 2d bounding box for this question "${input.whereToOverlay}"`
      )

      const element = boundingBox?.elements[0]!
      const overlay: ImageOverlay = {
        type: "image",
        src: hit.webformatURL,
        xmin: element.xmin,
        ymin: element.ymin,
        xmax: element.xmax,
        ymax: element.ymax,
      }

      return overlay
    },
  }),
]
