<?php

require_once __DIR__ . '/../core/cron_guard.php';
icylab_require_cron_access();

require_once __DIR__ . '/../core/config.php';
require_once __DIR__ . '/../core/database.php';
require_once __DIR__ . '/../services/notifications/NotificationService.php';

if (PHP_SAPI !== 'cli') header('Content-Type: text/plain; charset=utf-8');

$emailResult = array('processed' => 0, 'sent' => 0, 'failed' => 0);
try {
    $service = new NotificationService(db());
    // Este job solo vacia la cola. Alertas y cobranza se generan en daily_jobs.php.
    $emailResult = $service->processEmailQueue(100);
    echo 'process_notifications OK' . PHP_EOL;
} catch (Throwable $e) {
    error_log('process_notifications error: ' . $e->getMessage());
    if (PHP_SAPI !== 'cli') http_response_code(500);
    echo 'process_notifications ERROR' . PHP_EOL;
    echo $e->getMessage() . PHP_EOL;
}

echo 'Email processed: ' . (int)($emailResult['processed'] ?? 0) . PHP_EOL;
echo 'Email sent: ' . (int)($emailResult['sent'] ?? 0) . PHP_EOL;
echo 'Email failed: ' . (int)($emailResult['failed'] ?? 0) . PHP_EOL;
