import fs from "fs";
import path from "path";

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}`;

async function testVision() {
  console.log("Testing image input with deepseek/deepseek-v4-flash on OpenRouter...");

  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: "deepseek/deepseek-v4-flash",
      messages: [
        {
          role: "user",
          content: [
            { type: "text", text: "Welcher Händler steht auf diesem Beleg und wie lautet der Gesamtbetrag?" },
            {
              type: "image_url",
              image_url: {
                url: dataUrl
              }
            }
          ]
        }
      ]
    })
  });

  console.log("Status:", res.status, res.statusText);
  const data = await res.json();
  console.log("Response:", JSON.stringify(data, null, 2));
}

testVision().catch(console.error);
