import { prisma } from '../lib/prisma';
import { lexofficeService } from '../lib/lexoffice';

async function main() {
  console.log('🔍 Prüfe LexOffice Quotation Status...\n');

  // Hole die Test-Buchung
  const booking = await prisma.booking.findFirst({
    where: { bookingNumber: 'STM-2511-9237' },
    select: {
      id: true,
      bookingNumber: true,
      lexofficeOfferId: true,
    },
  });

  if (!booking) {
    console.log('❌ Buchung nicht gefunden');
    return;
  }

  console.log('📋 Buchung:', booking.bookingNumber);
  console.log('🆔 LexOffice Offer ID:', booking.lexofficeOfferId);

  if (!booking.lexofficeOfferId) {
    console.log('❌ Keine LexOffice Angebots-ID vorhanden');
    return;
  }

  try {
    console.log('\n🔍 Lade Quotation Details von LexOffice...');
    const quotation = await lexofficeService.getQuotation(booking.lexofficeOfferId);
    
    console.log('\n📊 Quotation Details:');
    console.log('  Voucher Number:', quotation.voucherNumber);
    console.log('  Created:', quotation.createdDate);
    console.log('  Updated:', quotation.updatedDate);
    console.log('\n  Full Response:', JSON.stringify(quotation, null, 2));

    console.log('\n📄 Versuche PDF Download...');
    const pdf = await lexofficeService.getQuotationPDF(booking.lexofficeOfferId);
    console.log('✅ PDF erfolgreich heruntergeladen! Größe:', pdf.length, 'bytes');
    
  } catch (error: any) {
    console.error('\n❌ Fehler:', error.message);
  }
}

main()
  .catch((e) => {
    console.error(e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
