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

const envContent = fs.readFileSync(".env.local", "utf-8");
const apiKeyMatch = envContent.match(/OPENROUTER_API_KEY=([^\r\n]+)/);
process.env.OPENROUTER_API_KEY = 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 testExtractorLive() {
  console.log("=== TESTING LIVE EXTRACTOR WITH OPENROUTER ===");
  const result = await extractReceiptData({
    base64DataUrl: dataUrl,
    fileName: "random_real_receipt_photo.jpg"
  });

  console.log("Extracted Merchant:", result.merchant.name);
  console.log("Extracted Gross Total:", result.totalAmount.value, result.currency);
  console.log("Extracted Date:", result.date.isoDate);
  console.log("Extracted Tax Breakdown:", JSON.stringify(result.taxBreakdown));
  console.log("Extracted Line Items:", JSON.stringify(result.lineItems));
  console.log("Validation:", JSON.stringify(result.validation));
}

testExtractorLive().catch(console.error);
