import { PrismaClient } from "@prisma/client";
import { generateText } from "ai";
import { z } from "zod";
import { generateStructuredObject } from "../lib/ai/generate-structured";
import { parseProviderApiKeys } from "../lib/ai/keys";
import { getModel } from "../lib/ai/provider";

async function main() {
  const prisma = new PrismaClient();
  const settings = await prisma.setting.findFirst();
  const apiKeys = parseProviderApiKeys(settings?.providerApiKeys);
  const schema = z.object({ answer: z.string(), score: z.number() });

  const models = [
    "deepseek-v4-flash",
    "deepseek-v4-pro",
    "minimax-m3",
    "minimax-m2.7",
    "kimi-k2.7-code",
    "glm-5.1",
    "qwen3.7-max",
  ];

  for (const modelId of models) {
    const model = getModel("opencode-go", modelId, apiKeys);
    process.stdout.write(`\n=== ${modelId} ===\n`);
    try {
      const { text } = await generateText({ model, prompt: "只回覆 OK" });
      console.log("generateText OK:", text.trim().slice(0, 60));
    } catch (error) {
      console.log("generateText FAIL:", error instanceof Error ? error.message : error);
    }

    try {
      const object = await generateStructuredObject({
        model,
        provider: "opencode-go",
        modelId,
        schema,
        prompt: "回傳 JSON：answer 一句話，score 為 0-100",
      });
      console.log("generateStructuredObject OK:", object);
    } catch (error) {
      console.log("generateStructuredObject FAIL:", error instanceof Error ? error.message : error);
    }
  }

  await prisma.$disconnect();
}

main().catch((error) => {
  console.error(error);
  process.exit(1);
});