import { PrismaClient } from '@prisma/client';
import { bookingAutomationService } from '../lib/booking-automation.ts';

const prisma = new PrismaClient();

async function testAutomation() {
  console.log('🧪 Teste Booking Automation...\n');

  try {
    // Hole neueste Buchung
    const latestBooking = await prisma.booking.findFirst({
      orderBy: { createdAt: 'desc' },
      select: {
        id: true,
        bookingNumber: true,
        customerName: true,
        eventDate: true,
      },
    });

    if (!latestBooking) {
      console.log('❌ Keine Buchung gefunden!');
      return;
    }

    console.log(`📋 Buchung: ${latestBooking.bookingNumber} - ${latestBooking.customerName}`);
    console.log(`   Event: ${new Date(latestBooking.eventDate).toLocaleDateString('de-DE')}\n`);

    console.log('🤖 Starte automatische Aktionen...\n');

    const result = await bookingAutomationService.runPostBookingActions(latestBooking.id);

    console.log('\n' + '─'.repeat(60));
    console.log('📊 Ergebnis:');
    console.log(`   ✅ E-Mail gesendet: ${result.emailSent ? 'Ja' : 'Nein'}`);
    console.log(`   ✅ Kalender synchronisiert: ${result.calendarSynced ? 'Ja' : 'Nein'}`);
    
    if (result.errors.length > 0) {
      console.log(`\n❌ Fehler (${result.errors.length}):`);
      result.errors.forEach(err => console.log(`   - ${err}`));
    } else {
      console.log('\n✅ Alle Aktionen erfolgreich!');
    }

  } catch (error: any) {
    console.error('\n❌ Fehler:', error.message);
    throw error;
  } finally {
    await prisma.$disconnect();
  }
}

testAutomation();
