<?php
/**
 * Diagnostic script to check why SMS notifications aren't being sent
 */
require_once 'config/database.php';
require_once 'includes/sms_helper.php';

echo "<h2>Bank Transaction SMS Notification Diagnostic</h2>\n";

try {
    // Check if non-musking SMS service is enabled
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'non_musking_enabled'");
    $stmt->execute();
    $result = $stmt->fetch();
    $nonMuskingEnabled = $result && $result['setting_value'] === '1';
    
    echo "<p><strong>Non-musking SMS Service Enabled:</strong> " . ($nonMuskingEnabled ? 'YES' : 'NO') . "</p>\n";
    
    // Check if bank transaction notifications are enabled
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'sms_send_bank_transaction_notifications'");
    $stmt->execute();
    $result = $stmt->fetch();
    $bankNotificationsEnabled = $result && $result['setting_value'] === '1';
    
    echo "<p><strong>Bank Transaction Notifications Enabled:</strong> " . ($bankNotificationsEnabled ? 'YES' : 'NO') . "</p>\n";
    
    // Check if MRAM gateway settings exist
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'mram_sms_username'");
    $stmt->execute();
    $result = $stmt->fetch();
    $mramUsernameExists = $result && !empty($result['setting_value']);
    
    echo "<p><strong>MRAM Gateway Username Configured:</strong> " . ($mramUsernameExists ? 'YES' : 'NO') . "</p>\n";
    
    // Check if MRAM password exists
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'mram_sms_password'");
    $stmt->execute();
    $result = $stmt->fetch();
    $mramPasswordExists = $result && !empty($result['setting_value']);
    
    echo "<p><strong>MRAM Gateway Password Configured:</strong> " . ($mramPasswordExists ? 'YES' : 'NO') . "</p>\n";
    
    // Check if sender ID exists
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'mram_sms_sender_id'");
    $stmt->execute();
    $result = $stmt->fetch();
    $mramSenderIdExists = $result && !empty($result['setting_value']);
    
    echo "<p><strong>MRAM Gateway Sender ID Configured:</strong> " . ($mramSenderIdExists ? 'YES' : 'NO') . "</p>\n";
    
    // Check if template exists
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'non_musking_templates_bank_transaction'");
    $stmt->execute();
    $result = $stmt->fetch();
    $templateExists = $result && !empty($result['setting_value']);
    
    echo "<p><strong>Bank Transaction Template Exists:</strong> " . ($templateExists ? 'YES' : 'NO') . "</p>\n";
    
    // Check if rejection template exists
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'non_musking_templates_bank_transaction_rejection'");
    $stmt->execute();
    $result = $stmt->fetch();
    $rejectionTemplateExists = $result && !empty($result['setting_value']);
    
    echo "<p><strong>Bank Transaction Rejection Template Exists:</strong> " . ($rejectionTemplateExists ? 'YES' : 'NO') . "</p>\n";
    
    // Display SMS configuration
    $cfg = sms_config();
    echo "<h3>SMS Configuration:</h3>\n";
    echo "<pre>" . print_r($cfg, true) . "</pre>\n";
    
    // Test SMS function
    echo "<h3>Testing SMS Functions:</h3>\n";
    if (function_exists('non_musking_send')) {
        echo "<p><strong>non_musking_send function exists:</strong> YES</p>\n";
    } else {
        echo "<p><strong>non_musking_send function exists:</strong> NO</p>\n";
    }
    
    if (function_exists('non_musking_send_template')) {
        echo "<p><strong>non_musking_send_template function exists:</strong> YES</p>\n";
    } else {
        echo "<p><strong>non_musking_send_template function exists:</strong> NO</p>\n";
    }
    
    // Show recent bank transactions to check if they have associated phone numbers
    echo "<h3>Recent Bank Transactions:</h3>\n";
    $stmt = $pdo->prepare("
        SELECT bt.*, s.phone, s.guardian_phone, s.first_name, s.last_name 
        FROM bank_transactions bt 
        JOIN students s ON bt.student_id = s.id 
        ORDER BY bt.submitted_at DESC 
        LIMIT 5
    ");
    $stmt->execute();
    $transactions = $stmt->fetchAll();
    
    foreach ($transactions as $transaction) {
        echo "<p><strong>Transaction:</strong> " . htmlspecialchars($transaction['transaction_id']) . 
             " | <strong>Amount:</strong> " . number_format($transaction['amount'], 2) . 
             " | <strong>Status:</strong> " . htmlspecialchars($transaction['status']) . 
             " | <strong>Student:</strong> " . htmlspecialchars($transaction['first_name'] . ' ' . $transaction['last_name']) .
             " | <strong>Phone:</strong> " . htmlspecialchars($transaction['phone'] ?? 'None') .
             " | <strong>Guardian Phone:</strong> " . htmlspecialchars($transaction['guardian_phone'] ?? 'None') . "</p>\n";
    }
    
    // Check for any recent SMS logs
    if (function_exists('sms_log')) {
        echo "<h3>Testing SMS Logging:</h3>\n";
        sms_log('Diagnostic test message');
        echo "<p>Test log message sent to SMS log</p>\n";
    } else {
        echo "<h3>SMS Logging:</h3>\n";
        echo "<p>sms_log function does not exist</p>\n";
    }
    
} catch (Exception $e) {
    echo "<p><strong>Error:</strong> " . $e->getMessage() . "</p>\n";
}

echo "<h3>Summary:</h3>\n";
echo "<p>For SMS notifications to work, the following must be true:</p>\n";
echo "<ul>\n";
echo "<li>Non-musking SMS service must be enabled</li>\n";
echo "<li>Bank transaction notifications must be enabled</li>\n";
echo "<li>MRAM gateway credentials must be configured</li>\n";
echo "<li>Templates must exist</li>\n";
echo "<li>Students must have phone numbers</li>\n";
echo "</ul>\n";
?>