<?php

declare(strict_types=1);

/**
 * bin/process_email_queue.php
 *
 * Run on a schedule (e.g. every minute via cron:
 *   * * * * * php /path/to/bin/process_email_queue.php >> storage/logs/queue-worker.log 2>&1
 * ). Claims due, pending email_queue rows and sends them via
 * MailService. Safe to run concurrently — EmailQueueModel::claimDue()
 * uses SELECT ... FOR UPDATE so two overlapping workers never send the
 * same row twice.
 */

require dirname(__DIR__) . '/vendor/autoload.php';
require dirname(__DIR__) . '/app/Core/Autoloader.php';

use App\Core\Autoloader;
use App\Core\Env;
use App\Services\EmailQueueService;

Autoloader::register(dirname(__DIR__));
Env::load(dirname(__DIR__) . '/.env');
require dirname(__DIR__) . '/app/Helpers/functions.php';

$result = (new EmailQueueService())->process((int) ($argv[1] ?? 50));

echo date('Y-m-d H:i:s') . " — sent: {$result['sent']}, failed: {$result['failed']}\n";
