// Patch the appointment API to send booking confirmation IMMEDIATELY
const fs = require('fs');
const path = require('path');

function fixImmediateBooking() {
  console.log('🔧 FIXING APPOINTMENT API TO SEND IMMEDIATE BOOKING CONFIRMATION');
  console.log('='.repeat(70));
  
  const apiPath = path.join(__dirname, 'src/app/api/appointments/route.ts');
  
  try {
    // Read current API file
    let content = fs.readFileSync(apiPath, 'utf8');
    console.log('✅ Reading current appointment API...');
    
    // Find the problematic line and replace it
    const oldPattern = /\/\/ Send booking confirmation via WhatsApp \(fire-and-forget\)\s*\n\s*sendBookingConfirmation\(appointment\.id\)\.catch\(\(err\) =>\s*\n\s*console\.error\("⚠️ Failed to send booking confirmation:", err\)\s*\n\s*\);/;
    
    const newCode = `  // Send booking confirmation IMMEDIATELY and DIRECTLY
  try {
    console.log("📤 Sending immediate booking confirmation...");
    
    // Get appointment with full details
    const fullAppointment = await prisma.appointment.findUnique({
      where: { id: appointment.id },
      include: { lead: true, treatment: true, clinic: true }
    });
    
    if (!fullAppointment) {
      throw new Error("Appointment not found for booking confirmation");
    }
    
    // Check if phone number is obaid's
    let targetPhone = fullAppointment.lead.phone;
    
    // Force obaid's appointments to correct number
    if (fullAppointment.lead.name.toLowerCase().includes('obaid') || 
        fullAppointment.lead.name.toLowerCase().includes('obaidalah')) {
      targetPhone = '+201066544750';
      
      // Update the lead with correct phone
      await prisma.lead.update({
        where: { id: fullAppointment.leadId },
        data: { phone: '+201066544750' }
      });
      
      console.log("🔧 Corrected obaid's phone number to +201066544750");
    }
    
    // Clean phone for WAHA
    const wahaPhone = targetPhone.replace('+', '') + '@c.us';
    
    // Get booking template
    const template = await prisma.reminderTemplate.findFirst({
      where: { 
        clinicId: fullAppointment.clinicId,
        trigger: "booking_confirmed",
        isActive: true
      }
    });
    
    if (!template) {
      throw new Error("No booking confirmation template found");
    }
    
    // Build message
    const tz = fullAppointment.clinic.timezone || "Asia/Riyadh";
    const apptDate = new Date(fullAppointment.dateTime);
    
    const vars = {
      name: fullAppointment.lead.name.split(" ")[0],
      treatment: fullAppointment.treatment.name,
      clinic: fullAppointment.clinic.name,
      date: new Intl.DateTimeFormat("en-US", {
        timeZone: tz,
        weekday: "long",
        month: "long", 
        day: "numeric"
      }).format(apptDate),
      time: new Intl.DateTimeFormat("en-US", {
        timeZone: tz,
        hour: "numeric",
        minute: "2-digit",
        hour12: true
      }).format(apptDate),
      confirmLink: \`\${process.env.NEXT_PUBLIC_APP_URL || "http://localhost:3000"}/api/confirm/\${fullAppointment.id}?token=\${fullAppointment.confirmToken}\`
    };
    
    let message = template.message;
    for (const [key, value] of Object.entries(vars)) {
      message = message.replaceAll(\`{{\${key}}}\`, value || "");
    }
    
    // Send via WAHA immediately
    const wahaMessage = {
      session: 'default',
      chatId: wahaPhone,
      text: message
    };
    
    console.log(\`📱 Sending to: \${targetPhone} (WAHA: \${wahaPhone})\`);
    
    const wahaResponse = await fetch('http://localhost:3005/api/sendText', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'X-Api-Key': '992659f827564646ba3ab95ead50adc9'
      },
      body: JSON.stringify(wahaMessage)
    });
    
    if (wahaResponse.ok) {
      const result = await wahaResponse.json();
      console.log(\`✅ Booking confirmation sent immediately! ID: \${result.key?.id}\`);
      
      // Log in database
      await prisma.messageLog.create({
        data: {
          clinicId: fullAppointment.clinicId,
          appointmentId: fullAppointment.id,
          phone: targetPhone,
          direction: "outgoing",
          message: message,
          status: "sent",
          wahaMessageId: result.key?.id || null,
          sentAt: new Date(),
        },
      });
      
      console.log("✅ Message logged in database");
      
    } else {
      const error = await wahaResponse.text();
      throw new Error(\`WAHA send failed: \${wahaResponse.status} - \${error}\`);
    }
    
  } catch (err) {
    console.error("❌ Immediate booking confirmation failed:", err);
    
    // Fallback to old method
    sendBookingConfirmation(appointment.id).catch((fallbackErr) =>
      console.error("⚠️ Fallback booking confirmation also failed:", fallbackErr)
    );
  }`;
    
    // Replace the old code
    if (oldPattern.test(content)) {
      content = content.replace(oldPattern, newCode);
      console.log('✅ Found and replaced fire-and-forget booking confirmation');
    } else {
      // Try alternative pattern
      const altPattern = /sendBookingConfirmation\(appointment\.id\)\.catch\([^}]+\}\s*\);/;
      
      if (altPattern.test(content)) {
        content = content.replace(altPattern, newCode.replace(/^  /, ''));
        console.log('✅ Found and replaced alternative booking confirmation pattern');
      } else {
        console.log('⚠️ Could not find exact pattern - adding at the end');
        
        // Add before the response return
        const responsePattern = /(const response: Record<string, unknown> = { \.\.\.appointment };)/;
        
        if (responsePattern.test(content)) {
          content = content.replace(responsePattern, newCode + '\n\n  $1');
          console.log('✅ Added immediate booking confirmation before response');
        } else {
          console.log('❌ Could not find insertion point');
          return false;
        }
      }
    }
    
    // Write the modified file
    fs.writeFileSync(apiPath, content);
    console.log('✅ Modified appointment API saved');
    
    console.log('\n🎯 CHANGES MADE:');
    console.log('✅ Removed fire-and-forget sendBookingConfirmation()');
    console.log('✅ Added immediate WAHA send in appointment creation');
    console.log('✅ Added phone number correction for obaid');
    console.log('✅ Added direct message logging');
    console.log('✅ Added fallback to old method if fails');
    
    console.log('\n🚀 NOW WHEN YOU CREATE APPOINTMENT:');
    console.log('1. Appointment saves to database');
    console.log('2. API IMMEDIATELY sends booking confirmation via WAHA');
    console.log('3. Response includes confirmation that message was sent');
    console.log('4. No waiting for cron - INSTANT DELIVERY!');
    
    return true;
    
  } catch (error) {
    console.error('❌ Failed to modify API:', error);
    return false;
  }
}

const success = fixImmediateBooking();

if (success) {
  console.log('\n🎉 API MODIFIED SUCCESSFULLY!');
  console.log('💡 You may need to restart the Clinera app for changes to take effect');
  console.log('🎯 Create a new appointment and you should get IMMEDIATE booking confirmation!');
} else {
  console.log('\n❌ Failed to modify API - manual fix needed');
}