import fs from "fs";

const envContent = fs.readFileSync(".env.local", "utf-8");
const apiKeyMatch = envContent.match(/OPENROUTER_API_KEY=([^\r\n]+)/);
const apiKey = apiKeyMatch ? apiKeyMatch[1].trim() : "";

const demoImgPath = "public/demo/01_aral_tankbeleg_muenchen.jpg";
const imgBase64 = fs.readFileSync(demoImgPath).toString("base64");
const dataUrl = `data:image/jpeg;base64,${imgBase64}`;

const candidateModels = [
  "google/gemini-2.0-flash-001",
  "openai/gpt-4o-mini",
  "qwen/qwen-2.5-vl-72b-instruct:free",
  "qwen/qwen-2.5-vl-72b-instruct",
  "meta-llama/llama-3.2-11b-vision-instruct",
];

async function findWorkingVisionModel() {
  for (const model of candidateModels) {
    console.log(`\nTesting model: ${model}...`);
    try {
      const res = await fetch("https://openrouter.ai/api/v1/chat/completions", {
        method: "POST",
        headers: {
          "Authorization": `Bearer ${apiKey}`,
          "Content-Type": "application/json",
          "HTTP-Referer": "http://localhost:3000",
          "X-Title": "Receipt Scanner",
        },
        body: JSON.stringify({
          model,
          messages: [
            {
              role: "user",
              content: [
                { type: "text", text: "Welcher Händler steht auf diesem Beleg und was ist die Gesamtsumme? Antworte kurz im JSON Format { merchant: string, total: number }" },
                {
                  type: "image_url",
                  image_url: {
                    url: dataUrl
                  }
                }
              ]
            }
          ]
        })
      });

      console.log(`Status: ${res.status} ${res.statusText}`);
      const data = await res.json();
      if (res.ok && data.choices && data.choices[0]) {
        console.log(`✓ SUCCESS with ${model}!`);
        console.log("Content:", data.choices[0].message.content);
        return model;
      } else {
        console.log("Error response:", JSON.stringify(data));
      }
    } catch (e) {
      console.log("Fetch error:", e.message);
    }
  }
}

findWorkingVisionModel().catch(console.error);
