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

async function main() {
  console.log('🔍 Teste LexOffice Artikel-Zugriff...\n');

  const testArticleIds = [
    '5d9d3716-f81e-4e46-b5cf-13988f489cc2', // Vintage ohne Flat
    '3f26c02c-d705-41a6-9b49-2e2e96e77036', // Vintage mit Flat
    '8954bd20-570c-4a7d-9ac3-8ee756652a89', // Nostalgie ohne Flat
    '701bd150-48c0-4937-b628-f4a754d86264', // Nostalgie mit Flat
  ];

  for (const articleId of testArticleIds) {
    try {
      console.log(`\n📦 Teste Artikel-ID: ${articleId}`);
      
      // Versuche den Artikel abzurufen
      const response = await fetch(`https://api.lexoffice.io/v1/articles/${articleId}`, {
        method: 'GET',
        headers: {
          'Authorization': `Bearer ${process.env.LEXOFFICE_API_KEY}`,
          'Accept': 'application/json',
        },
      });

      if (response.ok) {
        const article = await response.json();
        console.log('✅ Artikel gefunden:', article.title || article.name);
        console.log('   Preis:', article.price?.netAmount, 'EUR');
      } else {
        const error = await response.text();
        console.log('❌ Artikel nicht gefunden:', response.status, error);
      }
    } catch (error: any) {
      console.log('❌ Fehler:', error.message);
    }
  }

  console.log('\n\n🔍 Teste Quotation mit custom lineItem (ohne Artikel-ID)...\n');

  try {
    const testQuotation = {
      voucherDate: new Date().toISOString().split('T')[0] + 'T00:00:00.000+01:00',
      expirationDate: new Date(Date.now() + 14 * 24 * 60 * 60 * 1000).toISOString().split('T')[0] + 'T00:00:00.000+01:00',
      address: {
        name: 'Test Kunde',
        countryCode: 'DE',
      },
      lineItems: [
        {
          type: 'custom' as const,
          name: 'Test Fotobox VINTAGE_PHOTOS mit Druckflatrate',
          description: 'Test Beschreibung',
          quantity: 1,
          unitName: 'Stück',
          unitPrice: {
            currency: 'EUR',
            netAmount: 449,
            taxRatePercentage: 19,
          },
        },
      ],
      totalPrice: {
        currency: 'EUR',
      },
      taxConditions: {
        taxType: 'net' as const,
      },
    };

    console.log('📤 Erstelle Test-Quotation mit custom lineItem...');
    const result = await lexofficeService.createQuotation(testQuotation, true);
    console.log('✅ Erfolgreich erstellt:', result.id);
    console.log('   Voucher Number:', result.voucherNumber);

    console.log('\n📄 Teste PDF Download...');
    const pdf = await lexofficeService.getQuotationPDF(result.id);
    console.log('✅ PDF erfolgreich heruntergeladen! Größe:', pdf.length, 'bytes');

  } catch (error: any) {
    console.log('❌ Fehler:', error.message);
  }
}

main();
