import fs from "fs";
import { extractReceiptData } from "../src/lib/ai/extractor";

const content = fs.readFileSync(".env.local", "utf8");
for (const l of content.split("\n")) {
  const trimmed = l.trim();
  if (!trimmed || trimmed.startsWith("#")) continue;
  const idx = trimmed.indexOf("=");
  if (idx !== -1) {
    process.env[trimmed.slice(0, idx).trim()] = trimmed.slice(idx + 1).trim();
  }
}

async function test() {
  console.log("Model:", process.env.OPENROUTER_MODEL);
  console.log("Key prefix:", process.env.OPENROUTER_API_KEY?.substring(0, 10));
  
  // Test demo fallback
  const start = Date.now();
  try {
    const dummyBase64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==";
    const res = await extractReceiptData({ base64DataUrl: dummyBase64, fileName: "01_aral_tankbeleg.jpg" });
    console.log("Result in " + (Date.now() - start) + "ms:", res.merchant.name, res.totalAmount.value);
  } catch (e) {
    console.error("Test error:", e);
  }
}

test();
