<?php
// ============================================
// SMS HELPER - BulkSMS.bd Integration
// Path: /public_html/Appointment/opd/sms_helper.php
// ============================================

/**
 * Send SMS using BulkSMS.bd API
 * @param string $to - Recipient phone number (with country code)
 * @param string $message - SMS message
 * @return array - Response status
 */
function sendBulkSMS($to, $message) {
    // Your BulkSMS.bd credentials
    $api_key = "YOUR_API_KEY"; // Replace with your actual API key
    $sender_id = "8809617610000"; // Replace with your approved sender ID
    
    // API URL
    $url = "https://bulksmsbd.net/api/smsapi";
    
    $data = array(
        "api_key" => $api_key,
        "senderid" => $sender_id,
        "number" => $to,
        "message" => $message
    );
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    
    $response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $curl_error = curl_error($ch);
    curl_close($ch);
    
    // Log the response for debugging
    error_log("SMS Response - To: $to | Status: $http_code | Response: $response");
    
    if ($curl_error) {
        return array('success' => false, 'message' => 'CURL Error: ' . $curl_error);
    }
    
    if ($http_code == 200) {
        return array('success' => true, 'response' => $response);
    } else {
        return array('success' => false, 'message' => 'HTTP Error: ' . $http_code);
    }
}

/**
 * Send Appointment Confirmation to Patient
 */
function sendPatientConfirmation($patient_name, $patient_phone, $appointment_date, $appointment_time, $chamber_name, $chamber_address, $fee) {
    $message = "প্রিয় {$patient_name},\n";
    $message .= "আপনার অ্যাপয়েন্টমেন্ট নিশ্চিত করা হয়েছে।\n";
    $message .= "📅 তারিখ: " . date('d/m/Y', strtotime($appointment_date)) . "\n";
    $message .= "🕐 সময়: {$appointment_time}\n";
    $message .= "🏥 চেম্বার: {$chamber_name}\n";
    $message .= "📍 ঠিকানা: {$chamber_address}\n";
    $message .= "💰 ফি: ৳{$fee}\n";
    $message .= "ধন্যবাদ!\n";
    $message .= "- " . getBrandName();
    
    return sendBulkSMS($patient_phone, $message);
}

/**
 * Send Admin Notification for New Appointment
 */
function sendAdminNotification($patient_name, $patient_phone, $appointment_date, $appointment_time, $chamber_name, $category, $fee) {
    $admin_phone = "017XXXXXXXX"; // Replace with admin phone number
    
    $message = "📢 নতুন অ্যাপয়েন্টমেন্ট!\n";
    $message .= "👤 রোগী: {$patient_name}\n";
    $message .= "📱 মোবাইল: {$patient_phone}\n";
    $message .= "📅 তারিখ: " . date('d/m/Y', strtotime($appointment_date)) . "\n";
    $message .= "🕐 সময়: {$appointment_time}\n";
    $message .= "🏥 চেম্বার: {$chamber_name}\n";
    $message .= "📋 ক্যাটাগরি: {$category}\n";
    $message .= "💰 ফি: ৳{$fee}\n";
    $message .= "দয়া করে নিশ্চিত করুন।";
    
    return sendBulkSMS($admin_phone, $message);
}

/**
 * Get Brand Name from database or default
 */
function getBrandName() {
    global $conn;
    static $brand_name = null;
    
    if ($brand_name === null) {
        try {
            $result = mysqli_query($conn, "SELECT name FROM brand LIMIT 1");
            if ($result && mysqli_num_rows($result) > 0) {
                $row = mysqli_fetch_assoc($result);
                $brand_name = $row['name'];
            } else {
                $brand_name = 'ওয়ান হোমিওপ্যাথি';
            }
        } catch (Exception $e) {
            $brand_name = 'ওয়ান হোমিওপ্যাথি';
        }
    }
    
    return $brand_name;
}
?>