<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/notifications.php';

// This endpoint is called via AJAX to process the email queue in the background
// to avoid blocking the main UI thread.

ignore_user_abort(true);
set_time_limit(30);

try {
    $pdo = require __DIR__ . '/../config/database.php';
    $res = process_email_queue($pdo, 3); // Process 3 emails
    
    header('Content-Type: application/json');
    echo json_encode(['success' => true, 'processed' => $res]);
} catch (\Throwable $e) {
    http_response_code(500);
    echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}
