import fs from "fs";
import path from "path";
import os from "os";
import { generateReceiptPdf } from "../src/lib/export/pdfGenerator";
import { ProcessedReceipt } from "../src/lib/schema/receipt";

const sampleReceipt: ProcessedReceipt = {
  id: "rec-aral-1",
  imageHash: "hash-aral",
  originalFileName: "aral-tankstelle.jpg",
  fileSizeBytes: 204800,
  createdAt: "2026-08-18T10:00:00.000Z",
  updatedAt: "2026-08-18T10:00:00.000Z",
  status: "ready",
  merchant: {
    name: "Aral Tankstelle Station",
    address: "Musterstraße 12, 10115 Berlin",
    taxId: null,
    confidence: 0.98,
  },
  date: {
    isoDate: "2026-08-14",
    time: "14:22",
    confidence: 0.96,
  },
  documentType: "TANKBELEG",
  receiptNumber: "AR-982341",
  currency: "EUR",
  totalAmount: {
    value: 68.45,
    confidence: 0.99,
  },
  netAmount: 57.52,
  tipAmount: null,
  taxBreakdown: [
    {
      ratePercent: 19,
      taxAmount: 10.93,
      netAmount: 57.52,
    },
  ],
  lineItems: [
    {
      description: "Diesel",
      quantity: 27.46,
      unitPrice: 1.8,
      taxRate: 19,
      price: 49.43,
    },
    {
      description: "Shop Artikel",
      quantity: 1,
      unitPrice: null,
      taxRate: 19,
      price: 11.86,
    },
  ],
  suggestedCategory: "Tanken & KFZ",
  paymentMethod: "EC_KARTE",
  validation: {
    isMathValid: true,
    isDuplicateSuspected: false,
    needsUserReview: false,
    reviewField: "none",
    reviewReason: null,
    userConfirmed: false,
  },
} as ProcessedReceipt;

async function run() {
  const pdfBytes = await generateReceiptPdf([sampleReceipt], { locale: "de" });
  const downloadsDir = path.join(os.homedir(), "Downloads");
  const targetFile = path.join(downloadsDir, "Belege_Export_ScanReceipts.pdf");
  
  fs.writeFileSync(targetFile, Buffer.from(pdfBytes));
  console.log("SUCCESS: PDF saved to", targetFile);
}

run().catch((err) => {
  console.error("ERROR generating PDF:", err);
  process.exit(1);
});
