<?php
/**
 * Low Balance Alert Cron Job
 * 
 * This script should be run daily via cron job to check user balances
 * and send alerts to users with low balance.
 * 
 * Cron setup example (runs daily at 9 AM):
 * 0 9 * * * cd /path/to/bunleshub && php cron/low_balance_alerts.php
 */

// Set time limit for long operations
set_time_limit(300);

// Include required files
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../app/classes/LowBalanceAlert.php';

// Log start
error_log("Low Balance Alert Cron Job started at " . date('Y-m-d H:i:s'));

try {
    $lowBalanceAlert = new LowBalanceAlert($conn);
    
    // Process alerts
    $result = $lowBalanceAlert->processAlerts();
    
    // Log results
    error_log("Low Balance Alert Cron Job completed:");
    error_log("  - Total users needing alerts: {$result['total']}");
    error_log("  - Alerts sent successfully: {$result['sent']}");
    error_log("  - Alerts failed: {$result['failed']}");
    
    // Output for cron logs
    echo "Low Balance Alert Cron Job completed at " . date('Y-m-d H:i:s') . "\n";
    echo "Total users: {$result['total']}\n";
    echo "Sent: {$result['sent']}\n";
    echo "Failed: {$result['failed']}\n";
    
} catch (Exception $e) {
    error_log("Low Balance Alert Cron Job error: " . $e->getMessage());
    echo "Error: " . $e->getMessage() . "\n";
    exit(1);
}

exit(0);


