// In book_appointment.php - After booking is confirmed

// --- Send SMS ---
$sms_sent = false;
$sms_message = '';
$sms_details = array();

error_log("=== BOOKING SMS START ===");
error_log("ENABLE_SMS: " . (ENABLE_SMS ? 'true' : 'false'));
error_log("Patient: $patient_name, Phone: $patient_phone");
error_log("Doctor ID: $doctor_id, Chamber ID: $chamber_id");

if (ENABLE_SMS && !empty(SMS_API_KEY)) {
    try {
        // Get doctor details
        $doctor_query = "SELECT name FROM doctors WHERE id = $doctor_id";
        $doctor_result = mysqli_query($conn, $doctor_query);
        $doctor = mysqli_fetch_assoc($doctor_result);
        $doctor_name = isset($doctor['name']) ? $doctor['name'] : 'Doctor';
        error_log("Doctor found: $doctor_name");
        
        // Get chamber details
        $chamber_query = "SELECT name, address FROM chambers WHERE id = $chamber_id";
        $chamber_result = mysqli_query($conn, $chamber_query);
        $chamber = mysqli_fetch_assoc($chamber_result);
        $chamber_name = isset($chamber['name']) ? $chamber['name'] : 'Chamber';
        error_log("Chamber found: $chamber_name");
        
        // Format date and time
        $time_display = date('h:i A', strtotime($time_slot));
        $date_display = date('d-m-Y', strtotime($appointment_date));
        error_log("Date: $date_display, Time: $time_display");
        
        // Send SMS using the appointment function
        if (function_exists('sendAppointmentSMS')) {
            error_log("Calling sendAppointmentSMS...");
            $sms_results = sendAppointmentSMS(
                $patient_name,
                $patient_phone,
                $doctor_name,
                $chamber_name,
                $date_display,
                $time_display,
                $fee
            );
            
            $sms_details = $sms_results;
            
            if ($sms_results['patient'] && $sms_results['admin']) {
                $sms_sent = true;
                $sms_message = "SMS পাঠানো হয়েছে।";
                error_log("Both SMS sent successfully!");
            } else {
                if (!$sms_results['patient']) {
                    $sms_message = "রোগীকে SMS পাঠাতে সমস্যা হয়েছে। ";
                    error_log("Patient SMS failed");
                }
                if (!$sms_results['admin']) {
                    $sms_message .= "অ্যাডমিনকে SMS পাঠাতে সমস্যা হয়েছে।";
                    error_log("Admin SMS failed");
                }
                error_log("SMS partial failure: " . $sms_message);
            }
        } else {
            // Fallback: Direct send
            error_log("sendAppointmentSMS not found, using direct send");
            $patient_msg = "Dear $patient_name, Your appointment is confirmed for $date_display at $time_display at $chamber_name. Thank you.";
            $admin_msg = "New booking: $patient_name ($patient_phone) on $date_display at $time_display at $chamber_name.";
            
            $patient_result = sendSMS($patient_phone, $patient_msg);
            $admin_result = sendSMS(ADMIN_PHONE, $admin_msg);
            
            $sms_results = array('patient' => $patient_result, 'admin' => $admin_result);
            $sms_details = $sms_results;
            
            if ($patient_result && $admin_result) {
                $sms_sent = true;
                $sms_message = "SMS পাঠানো হয়েছে।";
                error_log("Direct SMS both sent successfully!");
            } else {
                $sms_message = "SMS পাঠাতে সমস্যা হয়েছে।";
                error_log("Direct SMS failed: patient=$patient_result, admin=$admin_result");
            }
        }
    } catch (Exception $e) {
        error_log("SMS Exception: " . $e->getMessage());
        $sms_message = "SMS পাঠাতে সমস্যা হয়েছে: " . $e->getMessage();
    }
} else {
    if (!ENABLE_SMS) {
        $sms_message = "SMS সেবা সক্রিয় নেই।";
        error_log("SMS is disabled");
    } else {
        $sms_message = "SMS API Key কনফিগার করা নেই।";
        error_log("SMS API Key missing or empty");
    }
}

error_log("SMS Final: sent=$sms_sent, message=$sms_message");
error_log("=== BOOKING SMS END ===");

// Send JSON response
echo json_encode(array(
    'success' => true,
    'message' => 'অ্যাপয়েন্টমেন্ট সফলভাবে বুক করা হয়েছে!',
    'sms_sent' => $sms_sent,
    'sms_message' => $sms_message,
    'sms_details' => $sms_details,
    'appointment_id' => $appointment_id
));
exit;