<?php
require_once __DIR__ . '/config/database.php';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // Check if the fee payment template exists
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'non_musking_tpl_fee_payment'");
    $stmt->execute();
    $result = $stmt->fetch();
    
    if ($result) {
        echo "✅ Fee payment template found: " . $result['setting_value'] . "\n";
    } else {
        echo "❌ Fee payment template not found\n";
    }
    
    // Check if non-musking is enabled
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'non_musking_enabled'");
    $stmt->execute();
    $result = $stmt->fetch();
    
    if ($result && $result['setting_value'] === '1') {
        echo "✅ Non-musking SMS is enabled\n";
    } else {
        echo "❌ Non-musking SMS is not enabled\n";
    }
    
    // Test sending a fee notification
    echo "\n--- Testing Fee Notification ---\n";
    
    require_once __DIR__ . '/includes/sms_helper.php';
    
    $testNumbers = ['+8801712345678']; // Test number
    $testVars = [
        'name' => 'Test Student',
        'paid_amount' => '500.00',
        'due_amount' => '0.00'
    ];
    
    $result = non_musking_send_template($pdo, 'fee_payment', $testVars, $testNumbers);
    
    echo "Full result: " . json_encode($result, JSON_PRETTY_PRINT) . "\n\n";
    
    if ($result['success']) {
        echo "✅ Test SMS sent successfully!\n";
    } else {
        echo "❌ Test SMS failed: " . $result['message'] . "\n";
        if (isset($result['raw_response'])) {
            echo "Raw response: " . $result['raw_response'] . "\n";
        }
    }
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>