// Schedule reminders for recent appointments
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

// Import the reminder scheduling function
const { scheduleReminders, sendBookingConfirmation } = require('./src/lib/reminders.ts');

async function main() {
  console.log('📅 Finding recent appointments...');
  
  const appointments = await prisma.appointment.findMany({
    where: {
      status: { in: ['scheduled', 'confirmed'] },
      dateTime: { gte: new Date() }
    },
    include: {
      clinic: true,
      lead: true,
      treatment: true
    },
    orderBy: { createdAt: 'desc' },
    take: 5
  });
  
  console.log(`Found ${appointments.length} appointments to schedule reminders for:`);
  
  for (const apt of appointments) {
    console.log(`📋 ${apt.lead.name} - ${apt.treatment.name} on ${apt.dateTime}`);
    
    try {
      await scheduleReminders(apt.id);
      console.log(`  ✅ Reminders scheduled`);
      
      // Send immediate booking confirmation
      await sendBookingConfirmation(apt.id);
      console.log(`  📨 Booking confirmation sent`);
      
    } catch (error) {
      console.log(`  ❌ Error: ${error.message}`);
    }
  }
  
  // Check final reminder count
  const reminderCount = await prisma.scheduledReminder.count({
    where: { status: 'pending' }
  });
  
  console.log(`\n📊 Total pending reminders: ${reminderCount}`);
  
  await prisma.$disconnect();
}

main().catch(console.error);